<pre name="code" class="csharp">using UnityEngine;
using System.Collections;
//现只适用 一行一列精灵图
public class SpriteAni : MonoBehaviour {
public float timer = 0; //切换下一帧 计时器
public float frameNumber = 30;//每秒多少帧
public int frameCounts = 3; //总帧
public int currentFrameIndex = 0; //当前帧
public float frameIndexOffsetX = 0.33333f; //== Tilling x 1/frameCounts动画总数
public float frameIndexOffsetY = 0.0f; //== Tilling y
private float currentFrameOffsetX = 0.0f;
private float currentFrameOffsetY = 0.0f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
timer += Time.deltaTime;
if(timer >= 1.0f/frameNumber)
{
currentFrameIndex ++;
timer -= 1.0f/frameNumber;
currentFrameIndex = currentFrameIndex%frameCounts;
currentFrameOffsetX = frameIndexOffsetX*currentFrameIndex;
currentFrameOffsetY = frameIndexOffsetY*currentFrameIndex;
this.renderer.material.SetTextureOffset("_MainTex", new Vector2(currentFrameOffsetX,currentFrameOffsetY));
}
}
}