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;
}
}
帧动画通用更新代码
最新推荐文章于 2024-05-06 17:56:03 发布