Is there any way to switch inspector into debug mode from code? I’d like to be able to toggle debug mode by shortcut.
2 Answers
2Managed to code it together for others who find this ![]()
Put this in a normal class (no monobehaviour) and the put the script in you editor folder, you can then use Alt+D to toggle the debug/normal mode on the inspector the mouse is over
[MenuItem("Tools/Toggle Inspector Mode &d")]//Change the shortcut here
static void ToggleInspectorDebug()
{
EditorWindow targetInspector = EditorWindow.mouseOverWindow; // "EditorWindow.focusedWindow" can be used instead
if (targetInspector != null && targetInspector.GetType().Name == "InspectorWindow")
{
Type type = Assembly.GetAssembly(typeof(UnityEditor.Editor)).GetType("UnityEditor.InspectorWindow"); //Get the type of the inspector window to find out the variable/method from
FieldInfo field = type.GetField("m_InspectorMode", BindingFlags.NonPublic | BindingFlags.Instance); //get the field we want to read, for the type (not our instance)
InspectorMode mode = (InspectorMode)field.GetValue(targetInspector); //read the value for our target inspector
mode = (mode == InspectorMode.Normal ? InspectorMode.Debug : InspectorMode.Normal); //toggle the value
//Debug.Log("New Inspector Mode: " + mode.ToString());
MethodInfo method = type.GetMethod("SetMode", BindingFlags.NonPublic | BindingFlags.Instance); //Find the method to change the mode for the type
method.Invoke(targetInspector, new object[] {mode}); //Call the function on our targetInspector, with the new mode as an object[]
targetInspector.Repaint(); //refresh inspector
}
}
The only flaw I had was that the inspector doesnt fully repaint, leaving variables with the ‘m_’ prefix, but just scrolling refreshes the inspector and its correct.
This was helpful: Shortcut Key for 'Lock Inspector' - Unity Engine - Unity Discussions
Also see menuItem docs: Unity - Scripting API: MenuItem
Worked using version 2018.2.5f1
Extra
Heres the script I use for my editor shortcuts:
- Clearing Console, Alt+C
- Locking Editor with mouse over, Alt+E
- Change inspector mode, Alt+D
Code:
using System;
using System.Reflection;
using UnityEditor;
namespace EditorImprovements
{
public class EditorUtilities
{
[MenuItem("Tools/Clear Console &c")]
static void ClearConsole()
{
// This simply does "LogEntries.Clear()" the long way:
var logEntries = Type.GetType("UnityEditor.LogEntries,UnityEditor.dll");
var clearMethod = logEntries.GetMethod("Clear", BindingFlags.Static | BindingFlags.Public);
clearMethod.Invoke(null,null);
}
[MenuItem("Tools/Toggle Inspector Lock (shortcut) &e")]
static void SelectLockableInspector()
{
EditorWindow inspectorToBeLocked = EditorWindow.mouseOverWindow; // "EditorWindow.focusedWindow" can be used instead
if (inspectorToBeLocked != null && inspectorToBeLocked.GetType().Name == "InspectorWindow")
{
Type type = Assembly.GetAssembly(typeof(UnityEditor.Editor)).GetType("UnityEditor.InspectorWindow");
PropertyInfo propertyInfo = type.GetProperty("isLocked");
bool value = (bool)propertyInfo.GetValue(inspectorToBeLocked, null);
propertyInfo.SetValue(inspectorToBeLocked, !value, null);
inspectorToBeLocked.Repaint();
}
}
[MenuItem("Tools/Toggle Inspector Mode &d")]//Change the shortcut here
static void ToggleInspectorDebug()
{
EditorWindow targetInspector = EditorWindow.mouseOverWindow; // "EditorWindow.focusedWindow" can be used instead
if (targetInspector != null && targetInspector.GetType().Name == "InspectorWindow")
{
Type type = Assembly.GetAssembly(typeof(UnityEditor.Editor)).GetType("UnityEditor.InspectorWindow"); //Get the type of the inspector window to find out the variable/method from
FieldInfo field = type.GetField("m_InspectorMode", BindingFlags.NonPublic | BindingFlags.Instance); //get the field we want to read, for the type (not our instance)
InspectorMode mode = (InspectorMode)field.GetValue(targetInspector); //read the value for our target inspector
mode = (mode == InspectorMode.Normal ? InspectorMode.Debug : InspectorMode.Normal); //toggle the value
//Debug.Log("New Inspector Mode: " + mode.ToString());
MethodInfo method = type.GetMethod("SetMode", BindingFlags.NonPublic | BindingFlags.Instance); //Find the method to change the mode for the type
method.Invoke(targetInspector, new object[] {mode}); //Call the function on our targetInspector, with the new mode as an object[]
targetInspector.Repaint(); //refresh inspector
}
}
}
}
Unfortunately only via reflection, as the InspectorWindow class is internal. (The field of interest is InspectorWindow.m_InspectorMode)
It seems you are right, thank you very much. However, probably better way (that's how I did it) is to access private method SetMode via Reflection.
– LittleCoyote
Awesome, thanks!
– Cobo3