Shortcut Key for 'Lock Inspector'

I’ve done a little bit of searching and can’t find a good, up to date, shortcut list that has this one listed, if it even exists.

I’m find myself constantly using the lock icon for the inspector so I keep the inspector locked on a Game Object and select a different object. Is there a shortcut key to toggle this option on and off so I don’t have to keep moving my mouse to that corner of the screen?

Thanks,
John

1 Like

Somebody who knows this might know about the same question for clearing the Console. Please tell us both! (I doubt either exists.)

this might be a little late, but I found a workaround using reflection :slight_smile:

using System;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEngine;
using System.Collections;
using Object = UnityEngine.Object;

public class InspectorLockToggle
{
    private static EditorWindow _mouseOverWindow;

    [MenuItem("Stuff/Select Inspector under mouse cursor (use hotkey) #&q")]
    static void SelectLockableInspector()
    {
        if (EditorWindow.mouseOverWindow.GetType().Name == "InspectorWindow")
        {
            _mouseOverWindow = EditorWindow.mouseOverWindow;
            Type type = Assembly.GetAssembly(typeof(Editor)).GetType("UnityEditor.InspectorWindow");
            Object[] findObjectsOfTypeAll = Resources.FindObjectsOfTypeAll(type);
            int indexOf = findObjectsOfTypeAll.ToList().IndexOf(_mouseOverWindow);
            EditorPrefs.SetInt("LockableInspectorIndex", indexOf);
        }
    }

    [MenuItem("Stuff/Toggle Lock &q")]
    static void ToggleInspectorLock()
    {
        if (_mouseOverWindow == null)
        {
            if (!EditorPrefs.HasKey("LockableInspectorIndex"))
                EditorPrefs.SetInt("LockableInspectorIndex", 0);
            int i = EditorPrefs.GetInt("LockableInspectorIndex");

            Type type = Assembly.GetAssembly(typeof(Editor)).GetType("UnityEditor.InspectorWindow");
            Object[] findObjectsOfTypeAll = Resources.FindObjectsOfTypeAll(type);
            _mouseOverWindow = (EditorWindow)findObjectsOfTypeAll[i];
        }

        if (_mouseOverWindow != null  _mouseOverWindow.GetType().Name == "InspectorWindow")
        {
            Type type = Assembly.GetAssembly(typeof(Editor)).GetType("UnityEditor.InspectorWindow");
            PropertyInfo propertyInfo = type.GetProperty("isLocked");
            bool value = (bool)propertyInfo.GetValue(_mouseOverWindow, null);
            propertyInfo.SetValue(_mouseOverWindow, !value, null);
            _mouseOverWindow.Repaint();
        }
    }

    [MenuItem("Stuff/Clear Console #&c")]
    static void ClearConsole()
    {
        Type type = Assembly.GetAssembly(typeof(Editor)).GetType("UnityEditorInternal.LogEntries");
        type.GetMethod("Clear").Invoke(null,null);
    }
}
9 Likes

Thanks a lot! I wonder why the lock command hasn’t been exposed. It’s really useful when displaying Handles in the OnSceneGUI. If you clic just a bit off you loose the gameObject selection and you have to select it again. It can get frustrating.

This is really useful code.

I’ve just ran into this. Thanks Abomb for the solution, saved me a lot of time.

Thanks A lot for the script.
it has 1 error at line 40
there is an && missing,
it has to be:

         if (_mouseOverWindow != null && _mouseOverWindow.GetType().Name == "InspectorWindow")

thx again, really helpful.

1 Like

Thanks A lot for the script.it has 1 error at line 40
there is an && missing, it has to be:

         if (_mouseOverWindow != null && _mouseOverWindow.GetType().Name == "InspectorWindow")

thx again, really helpful.

Great script - works perfectly. Thank you for sharing.

Just the thing I need. Thanks for the script Abomb.

very useful . thank you Abomb.
nb : for my students, simply add an “Editor” folder in Project et place this code. … nothing else
Locking and unlocking by pressing Alt + Q

you’re real clever, you!

1 Like

You can do it like this.

using UnityEditor;

static class EditorMenus
{
    // taken from: http://answers.unity3d.com/questions/282959/set-inspector-lock-by-code.html
    [MenuItem("Tools/Toggle Inspector Lock %l")] // Ctrl + L
    static void ToggleInspectorLock()
    {
        ActiveEditorTracker.sharedTracker.isLocked = !ActiveEditorTracker.sharedTracker.isLocked;
        ActiveEditorTracker.sharedTracker.ForceRebuild();
    }
}
18 Likes

@blueknee Thanks, works perfectly.

@blueknee Thanks, shorter and works :slight_smile:

Thanks both for the help, already using it with SHIFT + W (I found it quite comfortable this way :))

2 Likes

Replace %l with #w in @blueknee 's script if you want to assign to Shift W (https://docs.unity3d.com/ScriptReference/MenuItem.html)

3 Likes

Is this still working in 2018.2+? I can’t get this to work. I’m copying the code exactly and have made sure there are no key conflicts. Clicking in the Tools menu doesn’t work either.

EDIT: Beware of having 2 inspectors. My second one was hidden but that was the one being locked and unlocked.

Here’s my version of Abomb’s post above. Hover mouse over the inspector you want to lock/unlock then press the hotkey. If you’re testing this on an empty inspector it won’t work. You have to click a game object first, which makes sense, but I kept testing various inspector locking hotkey implementations on an empty inspector and the lock wasn’t changing. Stupid me.

using System;
using System.Reflection;
using UnityEditor;
public class InspectorLockToggle
{
    [MenuItem("Tools/Toggle Lock &q")]
    static void ToggleInspectorLock() // Inspector must be inspecting something to be locked
    {
        EditorWindow inspectorToBeLocked = EditorWindow.mouseOverWindow; // "EditorWindow.focusedWindow" can be used instead

        if (inspectorToBeLocked != null  && inspectorToBeLocked.GetType().Name == "InspectorWindow")
        {
            Type type = Assembly.GetAssembly(typeof(Editor)).GetType("UnityEditor.InspectorWindow");
            PropertyInfo propertyInfo = type.GetProperty("isLocked");
            bool value = (bool)propertyInfo.GetValue(inspectorToBeLocked, null);
            propertyInfo.SetValue(inspectorToBeLocked, !value, null);
            inspectorToBeLocked.Repaint();
        }
    }
}
2 Likes

Thanks Speedrun-Labs! only solution that worked for me with the mouse hovering (with 2018.2.5f1)

Heres the script I put together from this thread :slight_smile:

  • Clearing Console, Alt+C
  • Locking Editor with mouse over, Alt+E
  • Change inspector mode, Alt+D
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
            }
        }
    }
}
6 Likes