Unity3d UGUI序列帧动画

代码

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using System;

[RequireComponent(typeof(Image))]
public class UGUISpriteAnimation : MonoBehaviour
{
	private Image ImageSource;
	private int mCurFrame = 0;
	private float mDelta = 0;

	public float FPS = 5;
	public List<Sprite> SpriteFrames;
	public bool IsPlaying = false;
	public bool Foward = true;
	public bool AutoPlay = false;
	public bool Loop = false;

	public int FrameCount
	{
		get
		{
			return SpriteFrames.Count;
		}
	}

	void Awake()
	{
		ImageSource = GetComponent<Image>();
	}

	void Start()
	{
		if (AutoPlay)
		{
			Play();
		}
		else
		{
			IsPlaying = false;
		}
	}

	private void SetSprite(int idx)
	{
		ImageSource.sprite = SpriteFrames[idx];
		ImageSource.SetNativeSize();
	}

	public void Play()
	{
		IsPlaying = true;
		Foward = true;
	}

	public void PlayReverse()
	{
		IsPlaying = true;
		Foward = false;
	}

	void Update()
	{
		if (!IsPlaying || 0 == FrameCount)
		{
			return;
		}

		mDelta += Time.deltaTime;
		if (mDelta > 1 / FPS)
		{
			mDelta = 0;
			if(Foward)
			{
				mCurFrame++;
			}
			else
			{
				mCurFrame--;
			}

			if (mCurFrame >= FrameCount)
			{
				if (Loop)
				{
					mCurFrame = 0;
				}
				else
				{
					IsPlaying = false;
					return;
				}
			}
			else if (mCurFrame<0)
			{
				if (Loop)
				{
					mCurFrame = FrameCount-1;
				}
				else
				{
					IsPlaying = false;
					return;
				}
			}

			SetSprite(mCurFrame);
		}
	}

	public void Pause()
	{
		IsPlaying = false;
	}

	public void Resume()
	{
		if (!IsPlaying)
		{
			IsPlaying = true;
		}
	}

	public void Stop()
	{
		mCurFrame = 0;
		SetSprite(mCurFrame);
		IsPlaying = false;
	}

	public void Rewind()
	{
		mCurFrame = 0;
		SetSprite(mCurFrame);
		Play();
	}
}

  

时间: 2024-08-12 06:21:41

Unity3d UGUI序列帧动画的相关文章

Unity3D中播放序列帧动画

[狗刨学习网] class FrameAnimation { private float fps = 10.0f; private Rect drawPos; private float time = 0; private int currentIndex = 0; public void DrawFrameAnimation(Texture[] frameTex) { int length = frameTex.Length; GUI.DrawTexture(drawPos, frameTex

Unity3d UGUI 通用Confirm确认对话框实现(Inventory Pro学习总结)

背景 曾几何时,在Winform中,使用MessageBox对话框是如此happy,后来还有人封装了可以选择各种图标和带隐藏详情的MessageBox,现在Unity3d UGui就没有了这样的好事情了,所有的UI都需要自己来搞定了,幸好还有各种插件,Inventory Pro中的对话框方案不失一种通用,可复用的方案. YY(自己的想法) 所谓通用对话框,如果是自己实现的话有以下几点需要解决,窗体显示控制,窗体UI布局,窗体文字显示,窗体事件回调,窗体显示动画控制,窗体显示声音控制,窗体与其他窗

unity shader序列帧动画代码,顺便吐槽一下unity shader系统

http://www.cnblogs.com/hellohuan/archive/2014/01/10/3512784.html 一.看到UNITY论坛里有些人求unity shader序列帧动画,写shader我擅长啊,就顺势写了个CG的shader.代码很简单,就是变换UV采样序列帧贴图,美术配置行数列数以及变换速度. Shader "HELLOHUAN/Hello_Sequence" { Properties { _Color ("Main Color", C

Unity3D 中 Generic 动画导入设置和 Root Motion 之间的关系

2条评论 Unity3D 的 Mecanim 动画系统可以直接复用 3DS MAX 中制作的动画文件中的位移,这个就是通过 applyRootMotion 来达成的,我们只需要在使用 Animator 控制动画播放的同时,设置 Animator 的 applyRootMotion 字段为 True 就 OK 了. 那么怎么来利用这个特性达成我们想要的一些效果呢?这个 applyRootMotion 到底指的是啥呢? ApplyRootMotion,从字面上理解来看,是『应用根节点的运动』,听起来

Quick cocos2dx-Lua(V3.3R1)学习笔记(六)----让精灵动起来吧,使用序列帧动画

光有精灵太死板了,游戏里面肯定有不同的动画,难道人物走动,一张静态图就能打发对游戏质量要求越来越高的玩家么?所以我们要让精灵动起来 quick里面提供了使用序列帧动画的函数,我们直接使用就行了 现将大图载入缓存中,然后提取出来用即可 主要就是display,newFrames参数,这个有点string.format的味道 @function [parent=#display] newFrames -- @param string pattern 模式字符串 -- @param integer b

[Cocos2d-x v3.x]序列帧动画

简介 Cocos2d-x中,动画的具体内容是依靠精灵显示出来的,为了显示动态图片,我们需要不停切换精灵显示的内容,通过把静态的精灵变为动画播放器从而实现动画效果.动画由帧组成,每一帧都是一个纹理,我们可以使用一个纹理序列来创建动画. 我们使用Animation类描述一个动画,而精灵显示动画的动作则是一个Animate对象.动画动作Animate是精灵显示动画的动作,它由一个动画对象创建,并由精灵执行. 创建方法 - 手动添加序列帧到Animation类 - 使用文件初始化Animation类 手

iOS开发基础-序列帧动画之Tom猫

新建一个Single View Application,向该工程中导入Tom猫的图片资源,本示例演示Tom猫喝牛奶的动作.图片的名字为 drink_00.jpg.drink_01.jpg.....drink_80.jpg . 向 Main.storyboard 中添加 UIImageView ,将图片 drink_00.jpg 作为默认显示的画面.将该控件与 ViewController 类建立一个 IBOutlet 连接,属性名为: @property (weak, nonatomic) IB

1-10(退出键盘,设置按钮的背景图,transform属性,imageView序列帧动画,九宫格算法...)

(1-10目录) 1.取得2个文本输入框的值 2.退出键盘 3.设置按钮的背景图 4.执行动画 5.手动创建按钮以及设置按钮的一些基本属性 6 transform属性 7.imageView序列帧动画 8.九宫格算法 9.通过xib创建局部控件,利用模型封装加载数据 10设置状态栏的样式为白色 { 扯淡环节: 工作这么久了, 发现一些细节有时候总是忽略, 现在就把我刚接触时的知识点给整理出来!, 没事自己看看, 也分享给大家! 分享,创造快乐!哈哈 xq每天都生活在快乐当中, 愿你们也是! }

iOS开发-UI基础-汤姆猫Tom(序列帧动画)

使用UIImageView和UIButton实现Tom小案例. 功能分析:点击对应按钮后,tom实现相应的动作和声音 步骤分析: 搭建UI界面:(头部,肚子,尾巴,左脚和右脚使用的是button) 监听按钮点击 根据点击的按钮实现相应的序列帧动画 tom的动作就是一张一张的图片连续显示在屏幕上,实现动画有几种方式: 1.首尾式: //动画的开始 [UIView beginAnimations:nil context:nil]; //动画的内容 //提交动画(动画结束) [UIView commi