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
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]);
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);
}