/// <summary>
/// MIT License - Copyright(c) 2019 Ugo Belfiore
/// </summary>
using UnityEngine;
using UnityEditor;
using System.Reflection;
using System;
using UnityEditorInternal;
namespace ExtUnityComponents
{
/// <summary>
/// useful reflection methods
/// </summary>
public class ExtReflection
{
/// <summary>
/// play button on animator
/// </summary>
public static void SetPlayButton()
{
//open animator
System.Type animatorWindowType = null;
EditorWindow animatorWindow = OpenEditorWindow(AllNameAssemblyKnown.AnimatorControllerTool, ref animatorWindowType);
//open animation
System.Type animationWindowType = null;
EditorWindow animationWindowEditor = OpenEditorWindow(AllNameAssemblyKnown.AnimationWindow, ref animationWindowType);
//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));
//get the state of the "play" button
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]);
}
}
/// <summary>
/// for adding, do a GetAllEditorWindowTypes(true);
/// </summary>
public enum AllNameAssemblyKnown
{
BuildPlayerWindow,
ConsoleWindow,
IconSelector,
ObjectSelector,
ProjectBrowser,
ProjectTemplateWindow,
RagdollBuilder,
SceneHierarchySortingWindow,
SceneHierarchyWindow,
ScriptableWizard,
AddCurvesPopup,
AnimationWindow,
CurveEditorWindow,
MinMaxCurveEditorWindow,
AnnotationWindow,
LayerVisibilityWindow,
AssetStoreInstaBuyWindow,
AssetStoreLoginWindow,
AssetStoreWindow,
AudioMixerWindow,
CollabPublishDialog,
CollabCannotPublishDialog,
GameView,
AboutWindow,
AssetSaveDialog,
BumpMapSettingsFixingWindow,
ColorPicker,
EditorUpdateWindow,
FallbackEditorWindow,
GradientPicker,
PackageExport,
PackageImport,
PopupWindow,
PopupWindowWithoutFocus,
PragmaFixingWindow,
SaveWindowLayout,
DeleteWindowLayout,
EditorToolWindow,
SnapSettings,
TreeViewTestWindow,
GUIViewDebuggerWindow,
InspectorWindow,
PreviewWindow,
AddShaderVariantWindow,
AddComponentWindow,
AdvancedDropdownWindow,
LookDevView,
AttachToPlayerPlayerIPWindow,
HolographicEmulationWindow,
FrameDebuggerWindow,
SearchableEditorWindow,
LightingExplorerWindow,
LightingWindow,
LightmapPreviewWindow,
NavMeshEditorWindow,
OcclusionCullingWindow,
PhysicsDebugWindow,
TierSettingsWindow,
SceneView,
SettingsWindow,
ProjectSettingsWindow,
PreferenceSettingsWindow,
PackerWindow,
SpriteUtilityWindow,
TroubleshooterWindow,
UIElementsEditorWindowCreator,
UndoWindow,
UnityConnectConsentView,
UnityConnectEditorWindow,
MetroCertificatePasswordWindow,
MetroCreateTestCertificateWindow,
WindowChange,
WindowCheckoutFailure,
WindowPending,
WindowResolve,
WindowRevert,
WebViewEditorStaticWindow,
WebViewEditorWindow,
WebViewEditorWindowTabs,
SearchWindow,
LicenseManagementWindow,
PackageManagerWindow,
ParticleSystemWindow,
PresetSelector,
ProfilerWindow,
UISystemPreviewWindow,
ConflictResolverWindow,
DeleteShortcutProfileWindow,
PromptWindow,
ShortcutManagerWindow,
SketchUpImportDlg,
TerrainWizard,
ImportRawHeightmap,
ExportRawHeightmap,
TreeWizard,
DetailMeshWizard,
DetailTextureWizard,
PlaceTreeWizard,
FlattenHeightmap,
TestEditorWindow,
PanelDebugger,
UIElementsDebugger,
PainterSwitcherWindow,
AllocatorDebugger,
UIRDebugger,
UIElementsSamples,
AnimatorControllerTool,
LayerSettingsWindow,
ParameterControllerEditor,
AddStateMachineBehaviourComponentWindow,
AndroidKeystoreWindow,
TimelineWindow,
TMP_ProjectConversionUtility,
TMP_SpriteAssetImporter,
TMPro_FontAssetCreatorWindow,
CollabHistoryWindow,
CollabToolbarWindow,
TestRunnerWindow,
TMP_PackageResourceImporterWindow
}
public static System.Type GetEditorWindowTypeByName(string editorToFind, bool showDebug = false)
{
var result = new System.Collections.Generic.List<System.Type>();
System.Reflection.Assembly[] AS = System.AppDomain.CurrentDomain.GetAssemblies();
System.Type editorWindow = typeof(EditorWindow);
foreach (var A in AS)
{
System.Type[] types = A.GetTypes();
foreach (var T in types)
{
if (T.IsSubclassOf(editorWindow))
{
if (showDebug)
{
Debug.Log(T.Name);
}
if (T.Name.Equals(editorToFind))
{
return (T);
}
}
}
}
return (null);
}
public static FieldInfo[] GetAllFieldOfType(System.Type type, System.Reflection.BindingFlags bindings, bool showInConsol = false)
{
FieldInfo[] allField = type.GetFields(bindings);
if (showInConsol)
{
for (int i = 0; i < allField.Length; i++)
{
Debug.Log(allField[i].Name);
}
}
return (allField);
}
/// <summary>
/// Get all the most common Binding type of elements
/// </summary>
/// <returns></returns>
public static System.Reflection.BindingFlags GetFullBinding()
{
return (BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.NonPublic | BindingFlags.Static);
}
/// <summary>
/// from a given name, Open an EditorWindow.
/// you can also do:
/// EditorApplication.ExecuteMenuItem("Window/General/Hierarchy");
/// to open an known EditorWindow
///
/// To get the type of a known script, like UnityEditor.SceneHierarchyWindow, you can do also:
/// System.Type type = typeof(EditorWindow).Assembly.GetType("UnityEditor.SceneHierarchyWindow");
/// </summary>
/// <param name="editorWindowName">name of the editorWindow to open</param>
/// <param name="animationWindowType">type of the editorWindow (useful for others functions)</param>
/// <returns></returns>
public static EditorWindow OpenEditorWindow(AllNameAssemblyKnown editorWindowName, ref System.Type animationWindowType)
{
animationWindowType = GetEditorWindowTypeByName(editorWindowName.ToString());
EditorWindow animatorWindow = EditorWindow.GetWindow(animationWindowType);
return (animatorWindow);
}
}
}