Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
9
Question by Steven-Walker · Apr 23, 2014 at 01:09 AM · animationanimator

Get animation clip length using Animator?

I am working on a script that discreetly plays animation states, and that part is working. However I also need to get the length of the current clip but haven't found a way to do it.

Here's the code I'm using:

 Animator anim = obj.GetComponent<Animator>();
 if(anim != null) {
     anim.Play(track, -1);
     AnimatorStateInfo info = anim.GetCurrentAnimatorStateInfo(0);
     length = info.length;
     Debug.Log("Animation("+track+") :"+length);
     
     AnimationInfo[] clips = anim.GetCurrentAnimationClipState(0);
     Debug.Log("clips:"+clips.Length);
     foreach(AnimationInfo i in clips) {
         Debug.Log(i.clip.name+":"+i.clip.length);
     }
 }

The animation is playing fine, but the length from AnimatorStateInfo is 0, and AnimationInfo count is 0, both of which are incorrect. My Animator controller has 3 states. I have also tried the 'GetNext' versions of these methods with the same results.

Anybody know how to get the length of the specified clip at runtime?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

6 Replies

· Add your reply
  • Sort: 
avatar image
17
Best Answer

Answer by Steven-Walker · Apr 23, 2014 at 04:44 PM

EDIT: Found it! Turns out that the Motion type is derived from AnimationClip, so all that's required is a simple cast:

 Animator anim = obj.GetComponent<Animator>();
 if(anim != null) {
     UnityEditorInternal.AnimatorController ac = anim.runtimeAnimatorController as UnityEditorInternal.AnimatorController;
     UnityEditorInternal.StateMachine sm = ac.GetLayer(0).stateMachine;
     
     for(int i = 0; i < sm.stateCount; i++) {
         UnityEditorInternal.State state = sm.GetState(i);
         if(state.uniqueName == track) {
             AnimationClip clip = state.GetMotion() as AnimationClip;
             if(clip != null) {
                 length = clip.length;
             }
         }
     }
     Debug.Log("Animation:"+track+":"+length);
 }

Note also that the UnityEditorInternal object cannot be used in runtime code outside of the editor, so there is still no way to my knowledge of obtaining the AnimationClip at runtime from an Animator state. More explained here: http://forum.unity3d.com/threads/242478-Reading-keyframe-curve-data-from-Animator

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Zergling103 · Jan 31, 2015 at 01:14 AM 4
Share

The way the abstraction/black boxing of the Animator/$$anonymous$$ecanim system is set up is absolutely horrendous.

avatar image soltex1 · May 09, 2016 at 12:20 AM 0
Share

What if we want to set the length?

avatar image
43

Answer by Jesus-Perez-Felipe · May 04, 2015 at 11:10 PM

Using UnityEditorInternal.AnimatorController just work in Unity editor, for runtime I do like this:

 Animator anim = obj.GetComponent<Animator>();
 float time;
 RuntimeAnimatorController ac = anim[0].runtimeAnimatorController;    //Get Animator controller
     for(int i = 0; i<ac.animationClips.Length; i++)                 //For all animations
     {
         if(ac.animationClips[i].name == "AnimationName")        //If it has the same name as your clip
         {
             time = ac.animationClips[i].length;
         }
     }
 
Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image recagonlei · Jun 17, 2015 at 05:29 PM 0
Share

Perfect man! It's just what i wanted!!

Cheers!!

avatar image Kerwish · Dec 01, 2015 at 10:23 AM 2
Share
 RuntimeAnimatorController ac = anim[0].runtimeAnimatorController;

Why anim is table ([0]) ?

avatar image SebastianHertel Kerwish · Jul 13, 2020 at 11:09 AM 0
Share

It's a collection because there could be more than one animator attached.

avatar image blueknee · Mar 08, 2016 at 05:54 AM 3
Share

Thank you Felipe, I used this as a linq way. float time = anim.runtimeAnimatorController.animationClips.First(x => x.name == "AnimationName").length;

avatar image Xepherys blueknee · Sep 04, 2017 at 07:43 PM 0
Share

@blueknee - that's slick. Thank you!

avatar image immanuelsteiner blueknee · Sep 23, 2017 at 06:12 PM 0
Share

beeu-tee-ful!

avatar image paulsz · Aug 17, 2016 at 11:55 AM 0
Share

You da real $$anonymous$$VP :D

avatar image mnarimani · Jul 10, 2017 at 01:02 PM 0
Share

Thank you, it worked +1

Show more comments
avatar image
22

Answer by EvilWarren · Apr 23, 2015 at 12:05 PM

Exactly one year late to answer this question, so might be of little use now.

The current clip being played's length can be read, in your case, using anim.GetCurrentAnimationClipState(0).length with one caveat - it has to be done in the next frame. Not much info on it but probably due to execution order of animation update.

http://docs.unity3d.com/Manual/ExecutionOrder.html

I use a coroutine and wait for the end of frame. Just as an example:

 IEnumerator ShowCurrentClipLength()
 {
     yield return new WaitForEndOfFrame();
     print("current clip length = " + anim.GetCurrentAnimatorStateInfo(0).length);
 }

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mehdi14 · Jan 16, 2016 at 05:13 PM 0
Share

thanks ... you help me :)

avatar image modernator24 · Jun 18, 2017 at 04:03 AM 0
Share

Length, not length :)

avatar image EndallStrict modernator24 · Nov 21, 2017 at 04:47 PM 0
Share

length....

avatar image chrisoje · Sep 21, 2018 at 04:56 PM 0
Share

3 and a half years later, your answer is the best I found. The caveat you give is very important! WaitForEndOfFrame fixed my bug.

avatar image shahryar101 · Jan 25, 2021 at 01:54 AM 0
Share

This was helpful, thanks

avatar image BaraShiro · May 30 at 02:18 PM 0
Share

This works perfectly. This should be the best answer.

avatar image
1

Answer by belva1234 · Feb 16, 2018 at 11:26 AM

this will do the trick

 Animator anim = GetComponent<Animator>();
 float AnimationLength(string name) {
         float time = 0;
         RuntimeAnimatorController ac = anim.runtimeAnimatorController;   
 
         for (int i = 0; i < ac.animationClips.Length; i++)
             if (ac.animationClips[i].name == name)
                 time = ac.animationClips[i].length;
 
         return time;
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by cyborgjinx · Mar 27, 2018 at 02:22 PM

 Animator animComp = GetComponent<Animator>();
 animComp..GetCurrentAnimatorStateInfo(0).length;

AnimatorStateInfo Documentation

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

42 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Disable Root Motion in legacy humonoid animation 1 Answer

2D Animation Effects Velocity Wrong? 0 Answers

Trigger Animation Controller Parameter With Collider 1 Answer

Is it a way to get the animation object form the animator? 0 Answers

Error getting animation info with Animator 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges