隨筆-Unity中播放序列幀動畫

在使用NGUI的播放序列幀動畫組件時,在切出主程序再回來時會出現快速播放的問題。。因此本身寫了一套基於NGUI的播放序列幀動畫組件ide

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;
                    }
                }
            }
        }
    }
}
相關文章
相關標籤/搜索