精灵图片播放脚本
using UnityEngine;
using System.Collections;
public class DrawSprites : MonoBehaviour {
public Sprite[] MySprites;
private Sprite[] lastSprites;
private Sprite[] currentSprites;
private int index=0;
private float fps=10;
private float currentTime=0;
private PlayerState state;
private SpriteRenderer renderer=null;
void Start () {
currentSprites=MySprites;
lastSprites=currentSprites;
renderer=GetComponent<SpriteRenderer>();
}
void Update () {
state=PlayerState.Walk;
SetSprites(MySprites);
DrawSprite(currentSprites);
}
private void SetSprites(Sprite[] sprites)
{
currentSprites=sprites;
if(currentSprites!=lastSprites)
{
lastSprites=currentSprites;
index=0;
}
}
private void DrawSprite(Sprite[] sprites)
{
currentTime+=Time.deltaTime;
if(currentTime>1/fps){
index+=1;
currentTime=0;
if(index>=sprites.Length){
index=0;
}
}
renderer.sprite=sprites[index];
}
#region 角色状态枚举定义#
enum PlayerState
{
Walk,
Idle,
Attack
}
#endregion
}