Last active
March 14, 2025 10:40
-
-
Save EudenDev/51b6ccbf59d402872b10bafef4109103 to your computer and use it in GitHub Desktop.
Revisions
-
EudenDev revised this gist
Mar 14, 2025 . 1 changed file with 55 additions and 54 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,69 +4,70 @@ using UnityEditor.ShortcutManagement; using UnityEngine; public static class InspectorShortcuts { private static Type PropertyEditorType => typeof(EditorWindow).Assembly.GetType("UnityEditor.PropertyEditor"); private static Type UnityEditorInspectorType => typeof(Editor).Assembly.GetType("UnityEditor.InspectorWindow"); /// <summary> /// Opens a Unity Properties Editor using a keyboard shortcut. /// </summary> [Shortcut("Open Properties on Selection", KeyCode.F11)] private static void OpenPropertiesEditor() { // Get the method to open the Property Editor on the current selection MethodInfo method = PropertyEditorType.GetMethod("OpenPropertyEditorOnSelection", BindingFlags.Static | BindingFlags.NonPublic); // Ensure that the method is not null Debug.Assert(method != null, nameof(method) + " != null"); // Invoke the method to open the Property Editor method.Invoke(null, null); } /// <summary> /// Toggles the debug mode in the Unity Inspector window using the F12 key (by default). /// </summary> /// <remarks> /// Retrieves the Inspector window via reflection and toggles the InspectorMode between Normal and Debug modes. /// </remarks> [Shortcut("Toggle Inspector Debug Mode", KeyCode.F12)] private static void ToggleInspectorDebug() { var inspectors = GetExistingWindows(UnityEditorInspectorType); Array.ForEach(inspectors, ToggleDebugOnInspector); } private static void ToggleDebugOnInspector(EditorWindow inspectorWindow) { if (inspectorWindow == null || inspectorWindow.GetType().Name != "InspectorWindow") return; //get the field we want to read, for the type (not our instance) FieldInfo field = inspectorWindow.GetType().GetField("m_InspectorMode", BindingFlags.NonPublic | BindingFlags.Instance); //read the value for our target inspector Debug.Assert(field != null, nameof(field) + " != null"); var mode = (InspectorMode)field.GetValue(inspectorWindow); //toggle the value mode = mode == InspectorMode.Normal ? InspectorMode.Debug : InspectorMode.Normal; //Find the method to change the mode for the type MethodInfo method = UnityEditorInspectorType.GetMethod("SetMode", BindingFlags.NonPublic | BindingFlags.Instance); //Call the function on our targetInspector, with the new mode as an object[] Debug.Assert(method != null, nameof(method) + " != null"); method.Invoke(inspectorWindow, new object[] { mode }); // Refresh the Inspector window inspectorWindow.Repaint(); } // Helper method to get existing windows of a specific type private static EditorWindow[] GetExistingWindows(Type type) => Resources.FindObjectsOfTypeAll(type) as EditorWindow[]; } -
EudenDev created this gist
Mar 14, 2025 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,72 @@ using System; using System.Reflection; using UnityEditor; using UnityEditor.ShortcutManagement; using UnityEngine; public class InspectorShortcuts : MonoBehaviour { private static Type PropertyEditorType => typeof(EditorWindow).Assembly.GetType("UnityEditor.PropertyEditor"); private static Type UnityEditorInspectorType => typeof(Editor).Assembly.GetType("UnityEditor.InspectorWindow"); /// <summary> /// Opens a Unity Properties Editor using a keyboard shortcut. /// </summary> [Shortcut("Open Properties on Selection", KeyCode.F11)] private static void OpenPropertiesEditor() { // Get the method to open the Property Editor on the current selection MethodInfo method = PropertyEditorType.GetMethod("OpenPropertyEditorOnSelection", BindingFlags.Static | BindingFlags.NonPublic); // Ensure that the method is not null Debug.Assert(method != null, nameof(method) + " != null"); // Invoke the method to open the Property Editor method.Invoke(null, null); } /// <summary> /// Toggles the debug mode in the Unity Inspector window using the F12 key (by default). /// </summary> /// <remarks> /// Retrieves the Inspector window via reflection and toggles the InspectorMode between Normal and Debug modes. /// </remarks> [Shortcut("Toggle Inspector Debug Mode", KeyCode.F12)] private static void ToggleInspectorDebug() { var inspectors = GetExistingWindows(UnityEditorInspectorType); Array.ForEach(inspectors, ToggleDebugOnInspector); } private static void ToggleDebugOnInspector(EditorWindow inspectorWindow) { if (inspectorWindow == null || inspectorWindow.GetType().Name != "InspectorWindow") return; //get the field we want to read, for the type (not our instance) FieldInfo field = inspectorWindow.GetType().GetField("m_InspectorMode", BindingFlags.NonPublic | BindingFlags.Instance); //read the value for our target inspector Debug.Assert(field != null, nameof(field) + " != null"); var mode = (InspectorMode)field.GetValue(inspectorWindow); //toggle the value mode = mode == InspectorMode.Normal ? InspectorMode.Debug : InspectorMode.Normal; //Find the method to change the mode for the type MethodInfo method = UnityEditorInspectorType.GetMethod("SetMode", BindingFlags.NonPublic | BindingFlags.Instance); //Call the function on our targetInspector, with the new mode as an object[] Debug.Assert(method != null, nameof(method) + " != null"); method.Invoke(inspectorWindow, new object[] { mode }); // Refresh the Inspector window inspectorWindow.Repaint(); } // Helper method to get existing windows of a specific type private static EditorWindow[] GetExistingWindows(Type type) => Resources.FindObjectsOfTypeAll(type) as EditorWindow[]; }