Animator anim;
void MyMethod() {
anim.Play("myclip");
int clipLength = GetClipLength();
}
public int GetClipLength() {
return anim.GetCurrentAnimatorClipInfo(0)[0].clip.length;
}
Since the internal animation step hasn’t happened yet, “myclip” isn’t in the current clip info array. Even though Unity knows it’s going to get played next, I can’t seem to access a reference to it until that internal step happens. I understand I can set up a coroutine to grab it at end of frame, but that’s not really reliable in case the animator is told to play a different animation in the same frame for some separate reason or another.
From what I can tell it’s not possible unless I preemptively iterate over all the runtimeAnimatorController.animationClips and store them in a <string, int> with their name as the key and their index as the value in order to get the clip I want when I want. But that seems super dumb.
If anyone has a secret way of doing this, that’d be amazing.
Going through all the clips on startup wouldn’t even necessarily work because you need their state names, not the clip names.
My Animancer plugin would make this extremely easy. Instead of setting up a state and referring to it by name, you would just give your script the AnimationClip and play it directly.
It does work, but I’m just against the idea of having to populate an extra dictionary for every animating entity just for the sake of being able to get the length of one of its clips at will.
Though to be honest I’ve been on the brink of making an in-house animation controller for awhile now just to have more control over the clips themselves, and your asset might mean I don’t have to, thanks!
You wouldn’t necessarily need one per Animator, just one per AnimatorController. So you could have a Dictionary<RuntimeAnimatorController, Dictionary<string, float>>.
But yeah, I definitely recommend my plugin. The free version lets you try out all the features in the editor so you get to see everything it can do before you need to decide if it’s worth the money.