Shortcut Key for 'Lock Inspector'

Thanks Minchuilla and rest of thread, this is a massive QOL boost.

This work also for project view.

    public class LockMenu : UnityEditor.Editor
    {
        [MenuItem("Tools/Toggle Inspector Lock %l")] // Ctrl + L
        public static void ToggleInspectorLock()
        {
            EditorWindow inspectorToBeLocked = EditorWindow.mouseOverWindow; // "EditorWindow.focusedWindow" can be used instead

            Type projectBrowserType = Assembly.GetAssembly(typeof(UnityEditor.Editor)).GetType("UnityEditor.ProjectBrowser");

            Type inspectorWindowType = Assembly.GetAssembly(typeof(UnityEditor.Editor)).GetType("UnityEditor.InspectorWindow");

            PropertyInfo propertyInfo;
            if (inspectorToBeLocked.GetType() == projectBrowserType)
            {
                propertyInfo = projectBrowserType.GetProperty("isLocked", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
            }
            else if (inspectorToBeLocked.GetType() == inspectorWindowType)
            {
                propertyInfo = inspectorWindowType.GetProperty("isLocked");
            }
            else
            {
                return;
            }

            bool value = (bool)propertyInfo.GetValue(inspectorToBeLocked, null);
            propertyInfo.SetValue(inspectorToBeLocked, !value, null);
            inspectorToBeLocked.Repaint();
        }
    }
2 Likes

This one really Works!!! Thanks alot!

1 Like

None of this works for me in Unity 2019 2.21.f1,
Scrips compile and editor menus get added but the actual locking does not happen. Am I missing something obvious?

Are you hovering over the inspector window with the mouse?

1 Like

Have a look at C# Reflection, which essentially allows you to modify private variables and methods if you know their containing type and names. For that you can have a look at the UnityCsReference on GitHub. E.g. for locking the inspector, see UnityCsReference/Editor/Mono/Inspector/InspectorWindow.cs the isLocked property which corresponds to the code from above:

Type type = Assembly.GetAssembly(typeof(UnityEditor.Editor)).GetType("UnityEditor.InspectorWindow");
                PropertyInfo propertyInfo = type.GetProperty("isLocked");

To make this clear, reflection is the last resort. Your code will break if the variable name changes etc. Explore the public API first on the Unity docs to see what is possible.

1 Like

Can anyone create a shortcut to toggle “collapse all” in the inspector window?:frowning:
Or even better I want the inspector window to always toggle collapse all automatically. I tried referring to UnityCsReference on GitHub with no luck. thanks.

Awesome, thanks!

Also sometimes is useful to get access to object locked in Inspector. Here is example source code:

using UnityEngine;
using UnityEditor;
using System.Reflection;
using System;
using System.Collections;
using System.Collections.Generic;

public class EditorTools : EditorWindow
{
    [MenuItem("GameObject/Get Locked GameObject Name", false, 10)]
    public static void GetLockedGameObjectName()
    {
        GameObject lockedObject = GetLockedGameObject();
        Debug.Log(lockedObject.name);
    }

    static GameObject GetLockedGameObject() //return locked gameobject from Inspector Window or null value
    {
        Type type = typeof(EditorWindow).Assembly.GetType("UnityEditor.InspectorWindow");
        EditorWindow window = EditorWindow.GetWindow(type);
        FieldInfo fieldInfo = type.GetField("m_Tracker", BindingFlags.NonPublic | BindingFlags.Instance);
        ActiveEditorTracker tracker = fieldInfo.GetValue(window) as ActiveEditorTracker;
        MethodInfo methodInfo = tracker.GetType().GetMethod("GetObjectsLockedByThisTracker", BindingFlags.NonPublic | BindingFlags.Instance);
        List<UnityEngine.Object> lockedObjects = new List<UnityEngine.Object>();
        methodInfo.Invoke(tracker, new object[] {lockedObjects} );
        return (lockedObjects.Count > 0) ? (GameObject) lockedObjects[0] : null;
    }
}

nice…im using 2021.3.9f1 in an editor folder.

1 Like

Found it producing errors upon building so this would fix it :slight_smile:
And thank for the solution, I confirm it is woking in Unity 2021.3.25f

using UnityEditor;

#if UNITY_EDITOR
internal static class EditorMenus {
    [MenuItem("Tools/Toggle Inspector Lock %l")] // Ctrl + L
    static void ToggleInspectorLock() {
        ActiveEditorTracker.sharedTracker.isLocked = !ActiveEditorTracker.sharedTracker.isLocked;
        ActiveEditorTracker.sharedTracker.ForceRebuild();
    }
}
#endif

where did you guys learn about this # means shift & means alt and % means cltr like where did you learn this I haven’t seen a single documentation around this

1 Like

Hey, I’m on 2023.2.18f1 and this works to toggle the editor lock functionality, but the actual lock icon in the inspector UI doesn’t update.

image

I thought the ForceRebuild() might take care of that. I also tried looping over the ActiveEditorTracker.sharedTracker.activeEditors and calling Repaint() on each, but still nothing.

Any ideas how to get the UI to pick up the current lock state? Right now I’m just logging to Debug so you still know what’s going on, but that’s not ideal.