在使用NGUI的播放序列帧动画组件时,在切出主程序再回来时会出现快速播放的问题。。所以自己写了一套基于NGUI的播放序列帧动画组件
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class AnimationSprite : MonoBehaviour
{
/// <summary>
/// 是否播放动画
/// </summary>
public bool isPlay;
/// <summary>
/// 时间间隔
/// </summary>
public float time = 0.1f;
/// <summary>
/// 播放完成等待时间
/// </summary>
public float loopWait = 0;
/// <summary>
/// 是否循环
/// </summary>
public bool isLoop;
/// <summary>
/// 是否反向
/// </summary>
public bool back;
/// <summary>
/// 等待时间是否显示图片
/// </summary>
public bool waitSpriteEnabled;
private float nextFire;
private UISprite sprite;
private List<string> spriteName = new List<string>();
private int i = 0;
private float tempWait = 0;
[HideInInspector]
public bool isPlaying;
private void Start()
{
sprite = this.GetComponent<UISprite>();
foreach (string a in sprite.atlas.GetListOfSprites())
{
spriteName.Add(a);
}
tempWait = loopWait;
}
private void Update()
{
IsPlay(isPlay);
}
/// <summary>
/// 播放动画
/// </summary>
/// <param name="_isPlay"></param>
private void IsPlay(bool _isPlay)
{
if (!isPlaying)
{
if (back)
i = spriteName.Count-1;
else
i = 0;
}
if (_isPlay)
{
if (!back)
{
if (i < spriteName.Count)
{
sprite.enabled = true;
isPlaying = true;
if (Time.time > nextFire)
{
nextFire = Time.time + time;
sprite.spriteName = spriteName[i];
i++;
}
}
else
{
if (isLoop)
{
if (tempWait <= 0)
{
isPlay = true;
i = 0;
tempWait = loopWait;
}
else
{
tempWait -= Time.deltaTime;
if (!waitSpriteEnabled)
sprite.enabled = false;
}
}
else
{
isPlay = false;
isPlaying = false;
}
}
}
else
{
if (i > 0)
{
isPlaying = true;
sprite.enabled = true;
if (Time.time > nextFire)
{
nextFire = Time.time + time;
sprite.spriteName = spriteName[i];
i--;
}
}
else
{
if (isLoop)
{
if (tempWait <= 0)
{
isPlay = true;
i = spriteName.Count-1;
tempWait = loopWait;
}
else
{
tempWait -= Time.deltaTime;
if (!waitSpriteEnabled)
sprite.enabled = false;
}
}
else
{
isPlay = false;
isPlaying = false;
}
}
}
}
}
}