So i want to get the active animation clip from the animation editor:
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
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;
}
}