How do I iterate all animation clips used by an Animator?

Whilst MechAnim seems really great in most respects, it seems to unnecessarily hide an awful lot from us programmers…

Here’s a concrete example:

I would like to iterate all the animation clips used by an Animator, in the Awake() function in order to do some special case setup based on the lengths (in seconds) of various animations that it uses.

This isn’t possible with the current API - the best you can do is look at the current state and the next state (if there is one set).

This information only allows you to get at the animations that are currently being played - so if a state uses a blend tree and the weight is exactly 1.0 on just one of the animations in that blend tree, this will only allow you to get the information for that animation…

Then I thought “How silly of me! The animation clips are in the AnimationController. I obviously have to use that to access them.”

Except this doesn’t help, the runtimeAnimationController has more or less nothing in its interface. Certainly no way to iterate the layers, states. or motions it contains.

Then I found this thread: Getting a list of mecanim states in Animator - Unity Answers

this thread mentions using the UNDOCUMENTED UnityEditorInternal.AnimationController to access this information, but I need to use this code in the Awake() function which means that - whilst the game runs fine in the editor - I can’t build it.

The only option I can see that’s left to me (other than using the legacy animation system) is to make a custom serialised type to hold a description of the animation controller and use an associated custom property drawer to populate it from an animation controller.

This sounds like an insane amount of work to accomplish something which is - on the face of it - very simple: I just would like to iterate all animation clips used by an Animator.

Surely there’s a way to use an Animator to iterate all the layers, states, and motions of its associated AnimationController at runtime!?!

Does anyone know how?

Cheers, Alex

Animator anim;//make this a field
anim = GetComponent();

    foreach(AnimationClip ac in anim.runtimeAnimatorController.animationClips)
    {
       // look at all the animation clips here!
    }

AnimationClip arrclip = GetComponent().runtimeAnimatorController.animationClips;
foreach (AnimationClip clip in arrclip)
{
listNameClip.Add(clip.name);
}