NGUIAnimation 所支持的功能:
  • 是否循环播放
  • 是否加载时播放
  • 是否播放完成后销毁
  • 首次延迟播放
  • 循环播放一次后间隔多久再次播放
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
[RequireComponent(typeof(Image))]
public class NGUIAnimation : MonoBehaviour
{
    // 动画所需要的图
    public Sprite[] sprite_frame;
    // 帧动画播放的间隔时间
    public float duration = 0.1f;
    // 是否循环播放
    public bool is_loop = false;
    // 是否在加载的时候播放
    public bool play_onload = false;
    // 播放完成后动画是否销毁
    public bool isDestroy = true;
    // 延迟多少秒播放动画
    public float timeDelay = 0;
    // 如果是重复播放动画多少秒后在播放
    public float playTimeEndDelay = 0;
    private float played_time;
    private bool is_playing = false;
    // 当前针的图片
    private Image img;
    private bool currentDelay = false;
    private bool currentPlayDelay = false;
    void Start()
    {
        this.img = this.GetComponent<Image>();
        if (this.play_onload)
        {
            this.Play();
        }
    }
    public void Play()
    {
        if (this.is_loop)
        {
            this.play_loop();
        }
        else
        {
            this.play_once();
        }
    }
    void play_once()
    {
        if (this.sprite_frame.Length <= 1)
        {
            return;
        }
        if (timeDelay > 0)
        {
            currentDelay = true;
            Invoke("UpdateTimeDelayState", this.timeDelay);
        }
        this.played_time = 0;
        this.is_playing = true;
        this.is_loop = false;
    }
    void play_loop()
    {
        if (this.sprite_frame.Length <= 1)
        {
            return;
        }
        if (timeDelay > 0)
        {
            currentDelay = true;
            Invoke("UpdateTimeDelayState", this.timeDelay);
        }
        this.played_time = 0;
        this.is_playing = true;
        this.is_loop = true;
    }
    // 停止当前的动画播放
    public void Stop()
    {
        this.is_playing = false;
    }
    private void UpdateTimeDelayState()
    {
        currentDelay = false;
    }
    private void UpdatePlayTimeDelayState()
    {
        currentPlayDelay = false;
    }
    // Update is called once per frame
    void Update()
    {
        if (!this.is_playing)
        {
            return;
        }
        // TODO 如果延迟时间没有到, 直接返回
        if (currentDelay || currentPlayDelay)
        {
            return;
        }
        float dt = Time.deltaTime;
        this.played_time += dt;
        int index = (int)(this.played_time / this.duration);
        if (!this.is_loop)
        {
            if (index >= this.sprite_frame.Length)
            {
                this.is_playing = false;
                this.played_time = 0;
                if (isDestroy)
                {
                    Destroy(this.gameObject);
                }
            }
            else
            {
                this.img.sprite = this.sprite_frame[index];
            }
        }
        else
        {
            while (index >= this.sprite_frame.Length)
            {
                this.played_time -= (this.duration * this.sprite_frame.Length);
                index -= this.sprite_frame.Length;
                currentPlayDelay = true;
                Invoke("UpdatePlayTimeDelayState", this.playTimeEndDelay);
            }
            this.img.sprite = this.sprite_frame[index];
        }
    }
}