其实就是一个简单的计时器,到多少时间自动加载下一张图片,在判断个是否循环,说起来挺容易,自己想的时候还是想了好久哈哈,最后代码也不多,给大家分享下:
public class UIAnimation : MonoBehaviour
{
public Sprite[] UIArray;//图片数组
public bool Loop = false;//是否循环
private Image image;//载体Image
[Range(1, 100)]
public float playSpeed;//播放速度
private float interval;//间隔
private bool isRun = false;
private float timer;
// private bool isUpdate = true;
// Use this for initialization
void OnEnable()
{
image = GetComponent<Image>();
isRun = true;
StartCoroutine(TUI());
}
void Start()
{
interval = 1 / playSpeed;
}
IEnumerator TUI()
{
int i = 0;
while (isRun)
{
timer += Time.deltaTime;
if (timer >= interval)
{
timer = 0;
if (i < UIArray.Length)
{
image.sprite = UIArray[i++];
if (Loop && i >= UIArray.Length)
{
i = 0;
}
}
else
{
break;
}
}
yield return null;
}
}
}