Development environment: Unity5.6.2
private Animator animator;public void GetLengthByName(string name){ float length = 0; AnimationClip[] clips = animator.runtimeAnimatorController.animationClips; foreach(AnimationClip clip in clips) { if(clip.name.Equals(name) { length = clip.length; break; } } Debug.Log(length);}
As above, if you want to get the length of time (in seconds) of the animated segment in the animator you want to play, use:
AnimationClip[] clips = animator.runtimeAnimatorController.animationClips;
Note that the first use is
AnimatorClipInfo[] infos = animator.GetCurrentAnimatorClipInfo(0);
There is only one animation here, that is, the length of this array is 1, only the first animation, it may be as you run other animations, the members of this array will increase, but no eggs, ah, I want to get the desired length of the animation from the beginning, so this method is not available.
Then you have to use Runtimeanimatorcontroller.animationclips.
Also note that I play the animation with the Animator.play (string name)
The play method plays the state name in the state, but the name of the animationclip that hangs on the state is not necessarily the same
For the sake of convenience, the same name
In this way, iterate through the Animator.runtimeAnimatorController.animationClips to get the array, find the desired name, and get its length of time (in seconds).
Get the length of the animated segment in Animator in Unity