Getting current active animation clip from the animation editor window?

So i want to get the active animation clip from the animation editor:
5396502--547476--upload_2020-1-22_12-16-49.png

I found some help on another post but its not working, and I cannot figure out whats wrong. Unity returns:

MissingMethodException: UnityEditorInternal.AnimationWindowState.get_activeAnimationClip Due to: Attempted to access a missing member.

but when I try to print all the methods, the getter for ActiveAnimationClip is there! I even checked in UnityCsReference/blob/master/Editor/Mono/Animation/AnimationWindow/AnimationWindowState.cs
5396502--547491--upload_2020-1-22_12-26-27.png

Here is the code that I used for this:

using System;
using System.Reflection;
using UnityEditor;
using UnityEngine;
using Object = System.Object;

public class wAnimationWindowHelper
{
    [MenuItem("CustomStuff/PrintActiveClip")]
    public static void GetActiveClip()
    {
        GetAnimationWindowCurrentClip();
    }


    static System.Type animationWindowType = null;

    static System.Type GetAnimationWindowType()
    {
        if (animationWindowType == null)
        {
            animationWindowType = System.Type.GetType("UnityEditor.AnimationWindow,UnityEditor");
        }

        return animationWindowType;
    }

    static UnityEngine.Object GetOpenAnimationWindow()
    {
        UnityEngine.Object[] openAnimationWindows = Resources.FindObjectsOfTypeAll(GetAnimationWindowType());
        if (openAnimationWindows.Length > 0)
        {
            return openAnimationWindows[0];
        }

        return null;
    }


    static AnimationClip GetAnimationWindowCurrentClip()
    {
        UnityEngine.Object w = GetOpenAnimationWindow();
        if (w != null)
        {
            BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance;
            FieldInfo animEditor = GetAnimationWindowType().GetField("m_AnimEditor", flags);

            Type animEditorType = animEditor.FieldType;
            System.Object animEditorObject = animEditor.GetValue(w);
            FieldInfo animWindowState = animEditorType.GetField("m_State", flags);
            Type windowStateType = animWindowState.FieldType;

            System.Object clip = windowStateType.InvokeMember("get_activeAnimationClip",
                BindingFlags.InvokeMethod | BindingFlags.Public, null, animWindowState.GetValue(animEditorObject),
                null);

            Debug.Log(clip);
           

            return (AnimationClip) clip;
        }

        return null;
    }
}
1 Like

If you haven’t found a solution yet, this worked for me:
I replaced System.Object clip = windowStateType.InvokeMember("get_activeAnimationClip", BindingFlags.InvokeMethod | BindingFlags.Public, null, animWindowState.GetValue(animEditorObject), null);
with

PropertyInfo propInfo = windowStateType.GetProperty("activeAnimationClip");
System.Object clip = propInfo.GetValue(animWindowState.GetValue(animEditorObject));
3 Likes

Brilliant. This works. Thank you.

1 Like