帧动画通用更新代码

来源:互联网 发布:国内域名不备案 编辑:程序博客网 时间:2024/05/23 02:03
using UnityEngine;using System.Collections;public class animUpdate : MonoBehaviour {        public float animSpeed = 10;//1秒播放10帧    public float animTimeInteval = 0;    public float animTimer = 0;//计时器     public SpriteRenderer spriteRenderer;//渲染器    public Sprite[] SpriteArray;//帧数组    public int Index = 0;//记录播放第几帧    public int Length = 0;//数组长度    public bool isloop=false;    public bool isEnd = false;    public int loopTimes = 0;    // Use this for initialization    void Start()    {        animTimeInteval = 1 / animSpeed;//得到每一帧的时间间隔        Length = SpriteArray.Length;    }    // Update is called once per frame    void Update()    {        if(!isEnd)        {            animTimer += Time.deltaTime;            if (animTimer > animTimeInteval)            {                animTimer -= animTimeInteval;                spriteRenderer.sprite = SpriteArray[Index];                Index++;                if(isloop)                {                    Index %= Length;                }                else                {                    if (Index == Length)                    {                        if(loopTimes<=0)                        isEnd = true;                        loopTimes--;                    }                }            }        }    }    /// <summary>    /// reset to default value    /// </summary>    public void Reset()    {        Length = SpriteArray.Length;        animTimeInteval = 1 / animSpeed;        isEnd = false;        Index = 0;        animTimer = 0;    }}

原创粉丝点击