Unity3d the accurate playback duration of any animation clip by animator Animation state machine

Source: Internet
Author: User
Tags static class

Unity3d 4 and previous versions of the animated play with the animation, can be directly obtained its playback duration length. But the 5.x and later versions use animator to play the animation.

Https://docs.unity3d.com/Manual/AnimationOverview.html

While Mecanim are recommended for use with most situations, Unity have retained its legacy animation system which existed befo Re Unity 4. Need to use when working with older content created before Unity 4. For information on the Legacy animation system.

about how to get their playback duration under animator, a lot of people use getcurrentanimatorstateinfo and getnextanimatorstateinfo, but this is not the final verification solution.

The following is the correct, accurate posture for capturing the exact duration of any animation clip through the Animator Animation state machine:

 //Get animation state machine animator Play duration//@MarsZ December 19, 2017 20:46:20///site: Www.u3dnotes.compublic Static class Animatorext {public static float getcliplength (this Animator animator,string clip) {i F (null== Animator | | | string. IsNullOrEmpty (clip) | | null== Animator.runtimeanimatorcontroller) return 0; Runtimeanimatorcontroller ac = Animator.runtimeanimatorcontroller;  animationclip[] tanimationclips = ac.animationclips;if (Null = = Tanimationclips | | tanimationclips.length <= 0) return 0; Animationclip tanimationclip; for (int tcounter = 0, tLen = tanimationclips.length; tcounter < TLen; Tcounter + +) {TA Nimationclip = Ac.animationclips[i];if (null! = Tanimationclip && tanimationclip.name = = clip) return Tanimationclip.length;} return 0F;}}  

REF:
1, https://docs.unity3d.com/ScriptReference/Animator.html
2, https://docs.unity3d.com/ScriptReference/RuntimeAnimatorController.html
3, http://www.u3dnotes.com/archives/1131 (original initial address, reproduced please retain)

Unity3d the accurate playback duration of any animation clip by animator Animation state machine

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.