Last active
March 14, 2025 01:30
-
-
Save MattRix/3488c3aebadf4232c1ed58b483b4692a to your computer and use it in GitHub Desktop.
Put a debug mode button in the inspector and lets you toggle debug mode with alt+d
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
#if UNITY_EDITOR | |
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Reflection; | |
using UnityEditor; | |
using UnityEngine; | |
//UNFORTUNATELY this doesn't work right now because the inspector doesn't get refreshed properly in debug mode | |
//I'm sure it's fixable but I haven't bothered looking into it yet | |
[InitializeOnLoad] | |
public class InspectorHelper | |
{ | |
static Type inspectorType; | |
static UnityEngine.Object[] inspectorWindows; | |
static FieldInfo getModeField; | |
static MethodInfo setModeMethod; | |
static InspectorHelper() | |
{ | |
inspectorType = typeof(Editor).Assembly.GetType("UnityEditor.InspectorWindow"); | |
inspectorWindows = Resources.FindObjectsOfTypeAll(inspectorType); | |
getModeField = inspectorType.GetField("m_InspectorMode", BindingFlags.NonPublic | BindingFlags.Instance); | |
setModeMethod = inspectorType.GetMethod("SetMode", BindingFlags.NonPublic | BindingFlags.Instance); | |
Editor.finishedDefaultHeaderGUI += OnFinishedHeader; | |
} | |
private static void OnFinishedHeader(Editor editor) | |
{ | |
//var buttonStyle = new GUIStyle(GUI.skin.button) {padding = new RectOffset(2, 2, 2, 2) }; | |
bool isDebug = IsInspectorInDebugMode(); | |
var icon = isDebug ? EditorGUIUtility.IconContent("DebuggerEnabled") : EditorGUIUtility.IconContent("DebuggerDisabled"); | |
Rect rect = GUILayoutUtility.GetRect(30, 20); | |
GUILayout.Space(-rect.height); // Reclaim the reserved space | |
rect.y -= 15f; | |
rect.x += 5f; | |
rect.width = 30f; | |
if(GUI.Button(rect,icon)) | |
{ | |
//delay it so we don't redraw the inspector while we're drawing this UI stuff | |
EditorApplication.delayCall += ()=> | |
{ | |
isDebug = !isDebug; | |
SetInspectorMode(isDebug); | |
}; | |
} | |
} | |
public static bool IsInspectorInDebugMode() | |
{ | |
foreach (var window in inspectorWindows) | |
{ | |
var mode = (InspectorMode) getModeField.GetValue(window); | |
if (mode == InspectorMode.Debug) return true; | |
} | |
return false; | |
} | |
public static void SetInspectorMode(bool isDebug) | |
{ | |
foreach (EditorWindow window in inspectorWindows) | |
{ | |
var mode = isDebug ? InspectorMode.Debug : InspectorMode.Normal; | |
setModeMethod.Invoke(window, new object[] {mode}); | |
window.Repaint(); | |
} | |
} | |
[MenuItem("Tools/Toggle Inspector Mode &d")] | |
static void ToggleInspectorDebug() | |
{ | |
var isDebug = IsInspectorInDebugMode(); | |
SetInspectorMode(!isDebug); | |
} | |
[MenuItem("Tools/Toggle Inspector Lock &e")] | |
static void ToggleInspectorLock() | |
{ | |
var inspectorToBeLocked = EditorWindow.mouseOverWindow; | |
if (inspectorToBeLocked != null && inspectorToBeLocked.GetType().Name == "InspectorWindow") | |
{ | |
var propertyInfo = inspectorType.GetProperty("isLocked"); | |
bool value = (bool)propertyInfo.GetValue(inspectorToBeLocked, null); | |
propertyInfo.SetValue(inspectorToBeLocked, !value, null); | |
inspectorToBeLocked.Repaint(); | |
} | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment