Search Unity

Shortcut Key for 'Lock Inspector'

Discussion in 'Editor & General Support' started by johnknaack, Jul 7, 2011.

  1. johnknaack

    johnknaack

    Joined:
    Nov 14, 2010
    Posts:
    18
    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
     
  2. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    Somebody who knows this might know about the same question for clearing the Console. Please tell us both! (I doubt either exists.)
     
  3. Abomb

    Abomb

    Joined:
    Feb 20, 2010
    Posts:
    27
    this might be a little late, but I found a workaround using reflection :)

    Code (csharp):
    1. using System;
    2. using System.Linq;
    3. using System.Reflection;
    4. using UnityEditor;
    5. using UnityEngine;
    6. using System.Collections;
    7. using Object = UnityEngine.Object;
    8.  
    9. public class InspectorLockToggle
    10. {
    11.     private static EditorWindow _mouseOverWindow;
    12.  
    13.     [MenuItem("Stuff/Select Inspector under mouse cursor (use hotkey) #&q")]
    14.     static void SelectLockableInspector()
    15.     {
    16.         if (EditorWindow.mouseOverWindow.GetType().Name == "InspectorWindow")
    17.         {
    18.             _mouseOverWindow = EditorWindow.mouseOverWindow;
    19.             Type type = Assembly.GetAssembly(typeof(Editor)).GetType("UnityEditor.InspectorWindow");
    20.             Object[] findObjectsOfTypeAll = Resources.FindObjectsOfTypeAll(type);
    21.             int indexOf = findObjectsOfTypeAll.ToList().IndexOf(_mouseOverWindow);
    22.             EditorPrefs.SetInt("LockableInspectorIndex", indexOf);
    23.         }
    24.     }
    25.  
    26.     [MenuItem("Stuff/Toggle Lock &q")]
    27.     static void ToggleInspectorLock()
    28.     {
    29.         if (_mouseOverWindow == null)
    30.         {
    31.             if (!EditorPrefs.HasKey("LockableInspectorIndex"))
    32.                 EditorPrefs.SetInt("LockableInspectorIndex", 0);
    33.             int i = EditorPrefs.GetInt("LockableInspectorIndex");
    34.  
    35.             Type type = Assembly.GetAssembly(typeof(Editor)).GetType("UnityEditor.InspectorWindow");
    36.             Object[] findObjectsOfTypeAll = Resources.FindObjectsOfTypeAll(type);
    37.             _mouseOverWindow = (EditorWindow)findObjectsOfTypeAll[i];
    38.         }
    39.  
    40.         if (_mouseOverWindow != null  _mouseOverWindow.GetType().Name == "InspectorWindow")
    41.         {
    42.             Type type = Assembly.GetAssembly(typeof(Editor)).GetType("UnityEditor.InspectorWindow");
    43.             PropertyInfo propertyInfo = type.GetProperty("isLocked");
    44.             bool value = (bool)propertyInfo.GetValue(_mouseOverWindow, null);
    45.             propertyInfo.SetValue(_mouseOverWindow, !value, null);
    46.             _mouseOverWindow.Repaint();
    47.         }
    48.     }
    49.  
    50.     [MenuItem("Stuff/Clear Console #&c")]
    51.     static void ClearConsole()
    52.     {
    53.         Type type = Assembly.GetAssembly(typeof(Editor)).GetType("UnityEditorInternal.LogEntries");
    54.         type.GetMethod("Clear").Invoke(null,null);
    55.     }
    56. }
    57.  
     
    Last edited: Oct 9, 2012
  4. Razieln64

    Razieln64

    Joined:
    May 3, 2008
    Posts:
    129
    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.
     
    Last edited: Dec 23, 2012
  5. homeros

    homeros

    Joined:
    Dec 23, 2009
    Posts:
    121
    I've just ran into this. Thanks Abomb for the solution, saved me a lot of time.
     
  6. bendprogrammer

    bendprogrammer

    Joined:
    Apr 7, 2014
    Posts:
    5
    Thanks A lot for the script.
    it has 1 error at line 40
    there is an && missing,
    it has to be:
    Code (csharp):
    1.          if (_mouseOverWindow != null && _mouseOverWindow.GetType().Name == "InspectorWindow")
    thx again, really helpful.
     
    jojue likes this.
  7. bendprogrammer

    bendprogrammer

    Joined:
    Apr 7, 2014
    Posts:
    5
    Thanks A lot for the script.it has 1 error at line 40
    there is an && missing, it has to be:

    Code (csharp):
    1.          if (_mouseOverWindow != null && _mouseOverWindow.GetType().Name == "InspectorWindow")
    thx again, really helpful.
     
  8. Twice-Circled

    Twice-Circled

    Joined:
    Sep 29, 2013
    Posts:
    16
    Great script - works perfectly. Thank you for sharing.
     
  9. Cupal

    Cupal

    Joined:
    Jan 2, 2015
    Posts:
    2
    Just the thing I need. Thanks for the script Abomb.
     
  10. vincentFR

    vincentFR

    Joined:
    Jan 10, 2013
    Posts:
    14
    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
     
  11. Skjalg

    Skjalg

    Joined:
    May 25, 2009
    Posts:
    211
    you're real clever, you!
     
    Agent0023 likes this.
  12. blueknee

    blueknee

    Joined:
    Apr 5, 2014
    Posts:
    8
    You can do it like this.
    Code (csharp):
    1. using UnityEditor;
    2.  
    3. static class EditorMenus
    4. {
    5.     // taken from: http://answers.unity3d.com/questions/282959/set-inspector-lock-by-code.html
    6.     [MenuItem("Tools/Toggle Inspector Lock %l")] // Ctrl + L
    7.     static void ToggleInspectorLock()
    8.     {
    9.         ActiveEditorTracker.sharedTracker.isLocked = !ActiveEditorTracker.sharedTracker.isLocked;
    10.         ActiveEditorTracker.sharedTracker.ForceRebuild();
    11.     }
    12. }
    13.  
     
  13. SoulGameStudio

    SoulGameStudio

    Joined:
    Jan 18, 2016
    Posts:
    46
  14. Nukode

    Nukode

    Joined:
    May 24, 2016
    Posts:
    9
  15. Farresto

    Farresto

    Joined:
    Jan 20, 2016
    Posts:
    9
    Thanks both for the help, already using it with SHIFT + W (I found it quite comfortable this way :))
     
    Stradagee1 and Anisoropos like this.
  16. Anisoropos

    Anisoropos

    Joined:
    Jul 30, 2012
    Posts:
    102
  17. Romano

    Romano

    Joined:
    Nov 27, 2013
    Posts:
    76
    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.
     
    Last edited: Jan 31, 2018
  18. Speedrun-Labs

    Speedrun-Labs

    Joined:
    Aug 1, 2014
    Posts:
    16
    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.


    Code (CSharp):
    1. using System;
    2. using System.Reflection;
    3. using UnityEditor;
    4. public class InspectorLockToggle
    5. {
    6.     [MenuItem("Tools/Toggle Lock &q")]
    7.     static void ToggleInspectorLock() // Inspector must be inspecting something to be locked
    8.     {
    9.         EditorWindow inspectorToBeLocked = EditorWindow.mouseOverWindow; // "EditorWindow.focusedWindow" can be used instead
    10.  
    11.         if (inspectorToBeLocked != null  && inspectorToBeLocked.GetType().Name == "InspectorWindow")
    12.         {
    13.             Type type = Assembly.GetAssembly(typeof(Editor)).GetType("UnityEditor.InspectorWindow");
    14.             PropertyInfo propertyInfo = type.GetProperty("isLocked");
    15.             bool value = (bool)propertyInfo.GetValue(inspectorToBeLocked, null);
    16.             propertyInfo.SetValue(inspectorToBeLocked, !value, null);
    17.             inspectorToBeLocked.Repaint();
    18.         }
    19.     }
    20. }
     
    burton_qxp and Pincape like this.
  19. Minchuilla

    Minchuilla

    Joined:
    Feb 28, 2014
    Posts:
    16
    Thanks Speedrun-Labs! only solution that worked for me with the mouse hovering (with 2018.2.5f1)
     
  20. Minchuilla

    Minchuilla

    Joined:
    Feb 28, 2014
    Posts:
    16
    Heres the script I put together from this thread :)
    • Clearing Console, Alt+C
    • Locking Editor with mouse over, Alt+E
    • Change inspector mode, Alt+D

    Code (CSharp):
    1. using System;
    2. using System.Reflection;
    3. using UnityEditor;
    4.  
    5. namespace EditorImprovements
    6. {
    7.     public class EditorUtilities
    8.     {
    9.         [MenuItem("Tools/Clear Console &c")]
    10.         static void ClearConsole()
    11.         {
    12.             // This simply does "LogEntries.Clear()" the long way:
    13.             var logEntries = Type.GetType("UnityEditor.LogEntries,UnityEditor.dll");
    14.             var clearMethod = logEntries.GetMethod("Clear", BindingFlags.Static | BindingFlags.Public);
    15.             clearMethod.Invoke(null,null);
    16.         }
    17.  
    18.         [MenuItem("Tools/Toggle Inspector Lock (shortcut) &e")]
    19.         static void SelectLockableInspector()
    20.         {
    21.             EditorWindow inspectorToBeLocked = EditorWindow.mouseOverWindow; // "EditorWindow.focusedWindow" can be used instead
    22.             if (inspectorToBeLocked != null  && inspectorToBeLocked.GetType().Name == "InspectorWindow")
    23.             {
    24.                 Type type = Assembly.GetAssembly(typeof(UnityEditor.Editor)).GetType("UnityEditor.InspectorWindow");
    25.                 PropertyInfo propertyInfo = type.GetProperty("isLocked");
    26.                 bool value = (bool)propertyInfo.GetValue(inspectorToBeLocked, null);
    27.                 propertyInfo.SetValue(inspectorToBeLocked, !value, null);
    28.              
    29.                 inspectorToBeLocked.Repaint();
    30.             }
    31.         }
    32.  
    33.         [MenuItem("Tools/Toggle Inspector Mode &d")]//Change the shortcut here
    34.         static void ToggleInspectorDebug()
    35.         {
    36.             EditorWindow targetInspector = EditorWindow.mouseOverWindow; // "EditorWindow.focusedWindow" can be used instead
    37.             if (targetInspector != null  && targetInspector.GetType().Name == "InspectorWindow")
    38.             {
    39.                 Type type = Assembly.GetAssembly(typeof(UnityEditor.Editor)).GetType("UnityEditor.InspectorWindow");    //Get the type of the inspector window to find out the variable/method from
    40.                 FieldInfo field = type.GetField("m_InspectorMode", BindingFlags.NonPublic | BindingFlags.Instance);    //get the field we want to read, for the type (not our instance)
    41.              
    42.                 InspectorMode mode = (InspectorMode)field.GetValue(targetInspector);                                    //read the value for our target inspector
    43.                 mode = (mode == InspectorMode.Normal ? InspectorMode.Debug : InspectorMode.Normal);                    //toggle the value
    44.                 //Debug.Log("New Inspector Mode: " + mode.ToString());
    45.              
    46.                 MethodInfo method = type.GetMethod("SetMode", BindingFlags.NonPublic | BindingFlags.Instance);          //Find the method to change the mode for the type
    47.                 method.Invoke(targetInspector, new object[] {mode});                                                    //Call the function on our targetInspector, with the new mode as an object[]
    48.          
    49.                 targetInspector.Repaint();       //refresh inspector
    50.             }
    51.         }
    52.     }
    53. }
     
  21. madmart

    madmart

    Joined:
    Mar 10, 2014
    Posts:
    1
    Thanks Minchuilla and rest of thread, this is a massive QOL boost.
     
  22. Raikish

    Raikish

    Joined:
    Mar 19, 2019
    Posts:
    5
    This work also for project view.


    Code (CSharp):
    1.     public class LockMenu : UnityEditor.Editor
    2.     {
    3.         [MenuItem("Tools/Toggle Inspector Lock %l")] // Ctrl + L
    4.         public static void ToggleInspectorLock()
    5.         {
    6.             EditorWindow inspectorToBeLocked = EditorWindow.mouseOverWindow; // "EditorWindow.focusedWindow" can be used instead
    7.  
    8.             Type projectBrowserType = Assembly.GetAssembly(typeof(UnityEditor.Editor)).GetType("UnityEditor.ProjectBrowser");
    9.  
    10.             Type inspectorWindowType = Assembly.GetAssembly(typeof(UnityEditor.Editor)).GetType("UnityEditor.InspectorWindow");
    11.  
    12.             PropertyInfo propertyInfo;
    13.             if (inspectorToBeLocked.GetType() == projectBrowserType)
    14.             {
    15.                 propertyInfo = projectBrowserType.GetProperty("isLocked", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
    16.             }
    17.             else if (inspectorToBeLocked.GetType() == inspectorWindowType)
    18.             {
    19.                 propertyInfo = inspectorWindowType.GetProperty("isLocked");
    20.             }
    21.             else
    22.             {
    23.                 return;
    24.             }
    25.  
    26.             bool value = (bool)propertyInfo.GetValue(inspectorToBeLocked, null);
    27.             propertyInfo.SetValue(inspectorToBeLocked, !value, null);
    28.             inspectorToBeLocked.Repaint();
    29.         }
    30.     }
     
    sstese86 and Minchuilla like this.
  23. monstrXR

    monstrXR

    Joined:
    Feb 1, 2020
    Posts:
    1
    This one really Works!!! Thanks alot!
     
  24. sfilo

    sfilo

    Joined:
    Oct 23, 2012
    Posts:
    30
    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?
     
  25. Minchuilla

    Minchuilla

    Joined:
    Feb 28, 2014
    Posts:
    16
    Are you hovering over the inspector window with the mouse?
     
    Raikish likes this.
  26. Minchuilla

    Minchuilla

    Joined:
    Feb 28, 2014
    Posts:
    16
    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:
    Code (CSharp):
    1. Type type = Assembly.GetAssembly(typeof(UnityEditor.Editor)).GetType("UnityEditor.InspectorWindow");
    2.                 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.
     
    nandanramesh13 likes this.
  27. nandanramesh13

    nandanramesh13

    Joined:
    Sep 27, 2020
    Posts:
    4
    Can anyone create a shortcut to toggle "collapse all" in the inspector window?:(
    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.
     
  28. corbinyo

    corbinyo

    Joined:
    Aug 23, 2017
    Posts:
    26
    Awesome, thanks!
     
  29. Przemyslaw_Zaworski

    Przemyslaw_Zaworski

    Joined:
    Jun 9, 2017
    Posts:
    328
    Also sometimes is useful to get access to object locked in Inspector. Here is example source code:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.Reflection;
    4. using System;
    5. using System.Collections;
    6. using System.Collections.Generic;
    7.  
    8. public class EditorTools : EditorWindow
    9. {
    10.     [MenuItem("GameObject/Get Locked GameObject Name", false, 10)]
    11.     public static void GetLockedGameObjectName()
    12.     {
    13.         GameObject lockedObject = GetLockedGameObject();
    14.         Debug.Log(lockedObject.name);
    15.     }
    16.  
    17.     static GameObject GetLockedGameObject() //return locked gameobject from Inspector Window or null value
    18.     {
    19.         Type type = typeof(EditorWindow).Assembly.GetType("UnityEditor.InspectorWindow");
    20.         EditorWindow window = EditorWindow.GetWindow(type);
    21.         FieldInfo fieldInfo = type.GetField("m_Tracker", BindingFlags.NonPublic | BindingFlags.Instance);
    22.         ActiveEditorTracker tracker = fieldInfo.GetValue(window) as ActiveEditorTracker;
    23.         MethodInfo methodInfo = tracker.GetType().GetMethod("GetObjectsLockedByThisTracker", BindingFlags.NonPublic | BindingFlags.Instance);
    24.         List<UnityEngine.Object> lockedObjects = new List<UnityEngine.Object>();
    25.         methodInfo.Invoke(tracker, new object[] {lockedObjects} );
    26.         return (lockedObjects.Count > 0) ? (GameObject) lockedObjects[0] : null;
    27.     }
    28. }
     
  30. misaplay

    misaplay

    Joined:
    Aug 28, 2021
    Posts:
    3
    nice....im using 2021.3.9f1 in an editor folder.
     
    baraaelhalabi likes this.
  31. baraaelhalabi

    baraaelhalabi

    Joined:
    Oct 19, 2022
    Posts:
    8
    Found it producing errors upon building so this would fix it :)
    And thank for the solution, I confirm it is woking in Unity 2021.3.25f

    Code (CSharp):
    1. using UnityEditor;
    2.  
    3. #if UNITY_EDITOR
    4. internal static class EditorMenus {
    5.     [MenuItem("Tools/Toggle Inspector Lock %l")] // Ctrl + L
    6.     static void ToggleInspectorLock() {
    7.         ActiveEditorTracker.sharedTracker.isLocked = !ActiveEditorTracker.sharedTracker.isLocked;
    8.         ActiveEditorTracker.sharedTracker.ForceRebuild();
    9.     }
    10. }
    11. #endif