- Home /
How to Access the "play button" of a AnimatiorWindow from script editor
EDIT: My Goal is to find the method "play Button" or wathever the name is, and invoke it. I Woulld like to have some information about how, from a given unity editor window (here, the animation Window), access to all method, and have the information of the methods and their parameters that can be used by it.
Where can I find some documentation about it
Where to search in the unity source code to rapidly find, from a given goal, the method & parametter of a given EditorWindow. -
Here is how I get the Unity Animation Editor Window:
System.Reflection.Assembly editorAssembly = System.Reflection.Assembly.GetAssembly(typeof(EditorWindow));
System.Type animationWindowType = ExtReflexion.GetTypeFromAssembly("AnimationWindow", editorAssembly);
System.Object animationWindowObject = EditorWindow.GetWindow(animationWindowType);
here the function GetTypeFromAssembly:
public static System.Type GetTypeFromAssembly(string typeName, System.Reflection.Assembly assembly, System.StringComparison.ignoreCase = StringComparison.CurrentCultureIgnoreCase)
{
if (assembly == null)
return (null);
System.Type[] types = assembly.GetTypes();
foreach (System.Type type in types)
{
if (type.Name.Equals(typeName, ignoreCase) || type.Name.Contains('+' + typeName))
return (type);
}
return (null);
}
So, now I have my Type animationWindowObject. I have try to do
MethodInfo[] allMathod = animationWindowType.GetMethods()
and parse it, but I don't see the method I'm looking for.
I know I have to do something like:
System.Reflection.MethodInfo previewMethod = animationWindowType.GetMethod("PlayButton", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
and then previewMethod.Invoke
or something... but my knowledge is limited in that domain
I'm going to guess you don't actually need the animation window and you just want to play animations in editor mode. As such, I would read this article Playing animations from a custom EditorWindow
Thanks RobAnthem. In Fact I would like most generally if it is possible to access some function of the built in editor window, and I don't want to create my own editorWindow. I just want to execute some function of the built in editorWindow already present
Answer by Delvatron · Apr 11, 2019 at 12:55 PM
You should look into AnimEditor in the PlayButtonOnGUI method here
This line in particular:
controlInterface.StartPlayback();
Which you may be able to call with (Non tested pseudo code)
FieldfInfo animEditorFI =
animationWindowType.GetField("m_AnimEditor", BindingFlags.NonPublic | BindingFlags.Instance);
PropertyInfo controlInterfacePI =
animEditorFI.fieldType.GetProperty("controlInterface", BindingFlags.Public | BindingFlags.Instance);
MethodInfo playMI =
controlInterfacePI.propertyType.GetMethod("StartPlayback", BindingFlags.Public | BindingFlags.Instance);
object controlInterface =
controlInterfacePI.GetValue(animEditorFI.GetValue(animationWindowObject));
playMI.Invoke(controlInterface, new object[0]);
Answer by usernameHed · Apr 16, 2019 at 07:33 AM
Here, thanks to @valentin4311, my final static function for play/unplay a button (who use 3 other static function), all here:
/// <summary>
/// play button on animator
/// </summary>
public static void SetPlayButton()
{
//open Animation Editor Window
System.Type animationWindowType = null;
EditorWindow animationWindowEditor = ShowAndReturnEditorWindow("AnimationWindow", ref animationWindowType);
//Get animationWindow Type
animationWindowType = typeof(EditorWindow).Assembly.GetType("UnityEditor.AnimationWindow");
//Get field m_AnimEditor
FieldInfo animEditorFI = animationWindowType.GetField("m_AnimEditor", GetFullBinding());
//Get the propertue of animEditorFI
PropertyInfo controlInterfacePI = animEditorFI.FieldType.GetProperty("controlInterface", GetFullBinding());
//Get property i splaying or not
PropertyInfo isPlaying = controlInterfacePI.PropertyType.GetProperty("playing", GetFullBinding());
//get object controlInterface
object controlInterface = controlInterfacePI.GetValue(animEditorFI.GetValue(animationWindowEditor));
bool playing = (bool)isPlaying.GetValue(controlInterface);
if (!playing)
{
MethodInfo playMI = controlInterfacePI.PropertyType.GetMethod("StartPlayback", GetFullBinding());
playMI.Invoke(controlInterface, new object[0]);
}
else
{
MethodInfo playMI = controlInterfacePI.PropertyType.GetMethod("StopPlayback", GetFullBinding());
playMI.Invoke(controlInterface, new object[0]);
}
}
And here the other static function used:
public static System.Reflection.BindingFlags GetFullBinding()
{
return (BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.NonPublic | BindingFlags.Static);
}
//
/// <summary>
/// from a given name, return and open/show the editorWindow
/// usage:
/// System.Type animationWindowType = null;
/// EditorWindow animationWindowEditor = ShowAndReturnEditorWindow(ExtReflexion.AllNameAssemblyKnown.AnimationWindow, ref animationWindowType);
/// </summary>
public static EditorWindow ShowAndReturnEditorWindow(string editorWindow, ref System.Type animationWindowType)
{
System.Reflection.Assembly editorAssembly = System.Reflection.Assembly.GetAssembly(typeof(EditorWindow));
animationWindowType = GetTypeFromAssembly(editorWindow, editorAssembly);
EditorWindow animationWindowEditor = EditorWindow.GetWindow(animationWindowType);
return (animationWindowEditor);
}
//
/// <summary>
/// System.Reflection.Assembly editorAssembly = System.Reflection.Assembly.GetAssembly(typeof(EditorWindow));
/// GetTypeFromAssembly("AnimationWindow", editorAssembly);
/// </summary>
/// <returns></returns>
public static System.Type GetTypeFromAssembly(string typeName, System.Reflection.Assembly assembly, System.StringComparison ignoreCase = StringComparison.CurrentCultureIgnoreCase)
{
if (assembly == null)
return (null);
System.Type[] types = assembly.GetTypes();
foreach (System.Type type in types)
{
if (type.Name.Equals(typeName, ignoreCase) || type.Name.Contains('+' + typeName))
return (type);
}
return (null);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612194921im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to have two inspectors of different types thats objects they are inspecting change 0 Answers
EDITOR: How to Change Animation Curve Colors 0 Answers
Can I make animations snap to a frame? 1 Answer
Customize Vector3 Inspector 1 Answer
3D model doesn't render unless I look at it in scene view. 1 Answer