如何得到動畫幀數,控制unity動畫播放
阿新 • • 發佈:2021-12-07
在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()); }