Skip to content

Instantly share code, notes, and snippets.

@EudenDev
Last active March 14, 2025 10:40
Show Gist options
  • Save EudenDev/51b6ccbf59d402872b10bafef4109103 to your computer and use it in GitHub Desktop.
Save EudenDev/51b6ccbf59d402872b10bafef4109103 to your computer and use it in GitHub Desktop.

Revisions

  1. EudenDev revised this gist Mar 14, 2025. 1 changed file with 55 additions and 54 deletions.
    109 changes: 55 additions & 54 deletions InspectorShortcuts.cs
    Original file line number Diff line number Diff line change
    @@ -4,69 +4,70 @@
    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");
    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);
    /// <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");
    // 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);
    }
    // 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);
    }
    /// <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;
    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);
    //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);
    //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;
    //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);
    //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 });
    //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();
    }
    // 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[];
    }
    // Helper method to get existing windows of a specific type
    private static EditorWindow[] GetExistingWindows(Type type) =>
    Resources.FindObjectsOfTypeAll(type) as EditorWindow[];

    }
  2. EudenDev created this gist Mar 14, 2025.
    72 changes: 72 additions & 0 deletions InspectorShortcuts.cs
    Original 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[];
    }