随笔-Unity中播放序列帧动画

在使用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;
                    }
                }
            }
        }
    }
}

 

转载于:https://my.oschina.net/u/3184885/blog/1791605

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值