How to obtain the exact playback duration of any animation clip through Animator animation state machine in Unity3D
This article will explain in detail how to get the accurate playback duration of any animation clip through the Animator animation state machine in Unity3D. The editor thinks it is very practical, so I hope you can get something after reading this article.
The animation used for animation playback in Unity3d 4 and previous versions can directly obtain its playback duration. But animator is used to play animations in 5.x and later versions.
Https://docs.unity3d.com/Manual/AnimationOverview.html
While Mecanim is recommended for use in most situations, Unity has retained its legacy animation system which existed before Unity 4. You may need to use when working with older content created before Unity 4. For information on the Legacy animation system, see this section
Many people use GetCurrentAnimatorStateInfo and GetNextAnimatorStateInfo about how to obtain the playback duration under animator, but this is not the final verification solution.
The following is the correct and accurate pose for the exact playback duration of any animation clip obtained through the Animator animation state machine:
/ / obtain the duration of the animation clip of the animation state machine animator / @ MarsZ December 19, 2017 20:46:20///site:www.u3dnotes.compublic static class AnimatorExt {public static float GetClipLength (this Animator animator,string clip) {if (null== animator | | string.IsNullOrEmpty (clip) | | null== animator.runtimeAnimatorController) return 0; RuntimeAnimatorController ac = animator.runtimeAnimatorController AnimationClip [] tAnimationClips = ac.animationClips; if (null = = tAnimationClips | | tAnimationClips.Length