Get the animation length from any Animator State at runtime? Anyone figured this out?

I’ve read a billion threads back from the ancient times of unity till now.

How do I get some info about a state in the animator that is not currently running? Like length of the animation?

Scenario you have an animator with 3 states, Idle, Walk, Attack

Each of these states have an animation clip Bunny_Idle, Bunny_Walk, Bunny_SavageAttack

At runtime the Animator is playing Idle

While Idle is playing I want to know:

“what is the length of the animation that is attached to the Attack State?”

Note the name of the animations are different than name of states, which is the big issue

if your States names are the same as the animation clips then you are covered just fine. you get the RuntimeAnimationController which gives you an array of all the animations in the controller and you check the animation length which one you need. But if you have different animation names you are screwed.

Anyone figured this one?

Animator Controllers don’t let you do that. You basically have 3 options:

  • Rely on the clip names like you said.
  • Pull out all the info you need using an editor script and serialize it separately (probably a waste of time because it’s not going to be any cleaner than just relying on the names).
  • Give up on Animator Controllers and use a different animation system without such stupid limitations.

what a sad state this engine ended up

you may not like this but:

for the base animator, for each state we provides a dummy animation clip with the same name as the state.

then we use animator override controller, and override with our actual animations.

in runtime, we get animator.runtimeAnimatorController as AnimatorOverrideController.
then use animatorOverrideController[stateName] to get the clip and query its length

how this animatorOverrideController[stateName] works? why is an array?

I’ve ended up using playables

it is not an array, it is just a syntax that looks like accessing an array.

9861027--1420602--thank-you-master-blinky.gif

Incredible it actually works :hushed::sunglasses::p:smile::sunglasses::):):):):):hushed: