using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CommonSpriteAnimation : MonoBehaviour {
//精灵图片集合
public Sprite[] sprites;
//动画执行时间
public float animTime = 1f;
//精灵渲染器
private SpriteRenderer renderer;
//渲染图片位置
private int rendererPosition;
//渲染图片间隔
private float rendererTimer;
void Start () {
//获得精灵图片渲染器对象
renderer = GetComponent<SpriteRenderer> ();
}
/// <summary>
/// 获得动画执行时间
/// </summary>
/// <returns>The animation time.</returns>
public float getAnimationTime(){
return animTime;
}
/// <summary>
/// 执行动画
/// </summary>
public void startAnimation(){
//验证对象合法性
if(renderer == null || sprites == null || sprites.Length==0){
return;
}
//初始化参数: 位置、间隔
rendererPosition = 0;
rendererTimer = animTime / sprites.Length;
//开启协同执行动画
StartCoroutine (doAnimation());
}
/// <summary>
/// 执行动画:固定间隔,顺序渲染图片
/// </summary>
/// <returns>The animation.</returns>
IEnumerator doAnimation(){
while(rendererPosition < sprites.Length){
renderer.sprite = sprites[rendererPosition];
rendererPosition += 1;
yield return new WaitForSecondsRealtime (rendererTimer);
}
}
// //测试动画
// float t = 2f;
// float t1 = 2f;
// void Update(){
// if(t1>t){
// t1 = 0;
// startAnimation ();
// }
// t1 += Time.deltaTime;
// }
}
网友评论