一、直接上代码,(亲测有效)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class AnimationControl : MonoBehaviour
{
//动画组件
protected Animation anim;
public float timeStart = 0;//开始时间
public float timeEnd = 0;//结束时间
//动画名称;key-动画步骤名称 value-动画开始时间和结束时间
public Dictionary<string, int[]> animInfo = new Dictionary<string, int[]>();
private void Awake()
{
animInfo.Add("动画片段1",new int[2] {1,260 });
animInfo.Add("动画片段2", new int[2] {270,330 });
animInfo.Add("动画片段3", new int[2] {340,360 });
}
void Start()
{
StartCoroutine(Wait());
}
IEnumerator Wait()
{
int modelIndex = 0;
foreach (var item in animInfo)
{
modelIndex++;
timeStart = item.Value[0];
timeEnd = item.Value[1];
anim["Take 001"].time = timeStart / 25;//(25是动画的帧率)
anim.Play("Take 001");
//计算当前动画片段时间
float animTime = timeEnd - timeStart;
yield return new WaitForSeconds(animTime/25);
anim.Stop();
yield return new WaitForSeconds(4f);
}
}
}
|