Unity教程之-UGUI实现序列帧动画

unity new ui的动画系统已经很牛叉功能很全面了,不过对我等喜欢造锤子的程序员,还是喜欢自己实现一些东西。下面我们来看下ugui中序列帧动画的实现,

废话少说,上代码。 需要注意的是下面的类需要挂到一个有Image脚本的GameObject上,作为“shower”。


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

public class UMovie : MonoBehaviour
{
public string movieName;
public List<Sprite> lSprites;
public float fSep = 0.05f;

public float showerWidth
{
get
{
if (shower == null)
{
return 0;
}
return shower.rectTransform.rect.width;
}
}
public float showerHeight
{
get
{
if (shower == null)
{
return 0;
}
return shower.rectTransform.rect.height;
}
}

void Awake()
{
shower = GetComponent<Image>();

if (string.IsNullOrEmpty(movieName))
{
movieName = "movieName";
}
}
void Start()
{
Play(curFrame);
}

public void Play(int iFrame)
{
if (iFrame >= FrameCount)
{
iFrame = 0;
}
shower.sprite = lSprites[iFrame];
curFrame = iFrame;
shower.SetNativeSize();

if (dMovieEvents.ContainsKey(iFrame))
{
foreach (delegateMovieEvent del in dMovieEvents[iFrame])
{
del();
}
}
}

private Image shower;

int curFrame = 0;
public int FrameCount
{
get
{
return lSprites.Count;
}
}

float fDelta = 0;
void Update()
{
fDelta += Time.deltaTime;
if (fDelta > fSep)
{
fDelta = 0;
curFrame++;
Play(curFrame);
}
}

public delegate void delegateMovieEvent();
private Dictionary<int, List<delegateMovieEvent>> dMovieEvents = new Dictionary<int, List<delegateMovieEvent>>();
public void RegistMovieEvent(int frame, delegateMovieEvent delEvent)
{
if (!dMovieEvents.ContainsKey(frame))
{
dMovieEvents.Add(frame, new List<delegateMovieEvent>());
}
dMovieEvents[frame].Add(delEvent);
}
public void UnregistMovieEvent(int frame, delegateMovieEvent delEvent)
{
if (!dMovieEvents.ContainsKey(frame))
{
return;
}
if (dMovieEvents[frame].Contains(delEvent))
{
dMovieEvents[frame].Remove(delEvent);
}
}
}

好了,本篇unity教程到此结束!