Get the length of the animated segment in Animator in Unity

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.