Created
October 31, 2023 16:20
-
-
Save EudenDev/51ed0418db2c1adb34224872a732b7d1 to your computer and use it in GitHub Desktop.
Unity script to activate inspector debug mode from a keyboard shortcut. By default F12 but can be changed via Edit/Shortcuts...
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 characters
| using System; | |
| using System.Reflection; | |
| using UnityEditor; | |
| using UnityEditor.ShortcutManagement; | |
| using UnityEngine; | |
| using Debug = System.Diagnostics.Debug; | |
| public class InspectorToggleDebugShortcut : MonoBehaviour | |
| { | |
| private static Type UnityEditorInspectorType => typeof(Editor).Assembly.GetType("UnityEditor.InspectorWindow"); | |
| /// <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 InspectorDebug() | |
| { | |
| EditorWindow targetInspector = EditorWindow.GetWindow(UnityEditorInspectorType); | |
| if (targetInspector == null || targetInspector.GetType().Name != "InspectorWindow") return; | |
| FieldInfo field = | |
| UnityEditorInspectorType.GetField("m_InspectorMode", BindingFlags.NonPublic | BindingFlags.Instance); | |
| Debug.Assert(field != null, nameof(field) + " != null"); | |
| var mode = (InspectorMode)field.GetValue(targetInspector); | |
| mode = (mode == InspectorMode.Normal ? InspectorMode.Debug : InspectorMode.Normal); | |
| MethodInfo method = | |
| UnityEditorInspectorType.GetMethod("SetMode", BindingFlags.NonPublic | BindingFlags.Instance); | |
| Debug.Assert(method != null, nameof(method) + " != null"); | |
| method.Invoke(targetInspector, new object[] { mode }); | |
| // Refresh the Inspector window | |
| targetInspector.Repaint(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment