Unity 5.6 - Get list of animations
I'm making a local multiplayer game where animation controllers are assigned when selecting a character based on the player you chose.
My player script currently contains Invokes and animation events all over the place. It's hard to keep track of the code and on rare occations, weird bugs arise when several things happens at once. I'd like to do something about this, so I've started to replace my invokes with coroutines.
The problem is, I currently got functions for these 3 states.
1) Before an animation is playing.
2) While an animation is playing.
3) After an animation has finished.
State 1 and 2 are simple to resolve by using Coroutines and WaitForSeconds.
The 3rd one however is a problem I can't resolve. Unity changed so drastically in 5.6, so most of the examples and threads out there are outdated.
Some animations got different playback speeds and theres a huge chance for some of them to be changed later on in the development. I'd like to try and keep things neat and organized by having everything in one function, but to do so, I need to get the length of specific animations.
Thanks in advance.
Answer by SmackDan · May 19, 2017 at 03:56 PM
This literally had me trying for hours. Every example out there was complex and outdated, yet the solution I finally stumbled upon was incredibly simple. I'm really hoping they'd make this part of the API more clear in the future.
// Get the Animator
Animator anim = GetComponent<Animator>();
// Get a list of the animation clips
AnimationClip[] animationClips = anim.runtimeAnimatorController.animationClips;
// Iterate over the clips and gather their information
foreach(AnimationClip animClip in animationClips)
{
Debug.Log(animClip.name + ": " + animClip.length);
}
Your answer
Follow this Question
Related Questions
Confused about animations 1 Answer
Animator Check the end of an animation 1 Answer
Syncing Animations both playing from Seperate Animators 0 Answers
Animator is not firing transitions. 0 Answers
NEED HELP ANIMATING PLAYER MOVEMENT 0 Answers