在Unity动画控制中,如何获得当前动画已经播放到的帧呢?
其实可以通过 clip.frameRate,clip.length,及normalizedTime来计算出具体的帧。
//当前动画机播放时长 currentTime = anim.GetCurrentAnimatorStateInfo(0).normalizedTime; //动画片段长度 float length = anim.GetCurrentAnimatorClipInfo(0)[0].clip.length; //获取动画片段帧频 float frameRate = anim.GetCurrentAnimatorClipInfo(0)[0].clip.frameRate; //计算动画片段总帧数 float totalFrame = length / (1 / frameRate); //计算当前播放的动画片段运行至哪一帧 int currentFrame = (int)(Mathf.Floor(totalFrame * clipTime) % totalFrame);(
clipTime改为currentTime
) Debug.Log(" Frame: " + currentFrame + “/” + totalFrame);
float Remap(float tex, float min, float max, float slefMin = 0, float slefMax = 1) { return (min + (tex - slefMin) * (max - min) / (slefMax - slefMin)); } IEnumerator OnUpdate() { while (true) { yield return new WaitForEndOfFrame(); //当前动画播放时长 float currentTime = anim.GetCurrentAnimatorStateInfo(0).normalizedTime; //总时长 float length = animLength[SelectType.ToString()].length; //总帧数 length / (1 / tempclip.frameRate) float totalFrame=animLength[SelectType.ToString()].totalFrame; //播放时长映射成帧数 int currentFrame =(int) Remap(currentTime, 0, totalFrame); if (currentFrame >= totalFrame) { break; } } Debug.Log("结束"); GameRoot.Instance.StopCoroutine(OnUpdate()); }