Last active
February 24, 2022 19:45
-
-
Save CapitanLiteral/0472d5e9f8e21dcbae6f2a81e2c6dbaf to your computer and use it in GitHub Desktop.
This file contains 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
using System; | |
using System.Reflection; | |
using UnityEditor; | |
namespace Plugins.Gists.Editor | |
{ | |
public static class InspectorLockToggle | |
{ | |
[MenuItem("Tools/Toggle Lock &q")] | |
static void ToggleWindowLock() // Inspector must be inspecting something to be locked | |
{ | |
EditorWindow windowToBeLocked = EditorWindow.mouseOverWindow; // "EditorWindow.focusedWindow" can be used instead | |
if (windowToBeLocked != null && windowToBeLocked.GetType().Name == "InspectorWindow") | |
{ | |
Type type = Assembly.GetAssembly(typeof(UnityEditor.Editor)).GetType("UnityEditor.InspectorWindow"); | |
PropertyInfo propertyInfo = type.GetProperty("isLocked"); | |
bool value = (bool)propertyInfo.GetValue(windowToBeLocked, null); | |
propertyInfo.SetValue(windowToBeLocked, !value, null); | |
windowToBeLocked.Repaint(); | |
} | |
else if (windowToBeLocked != null && windowToBeLocked.GetType().Name == "ProjectBrowser") | |
{ | |
Type type = Assembly.GetAssembly(typeof(UnityEditor.Editor)).GetType("UnityEditor.ProjectBrowser"); | |
PropertyInfo propertyInfo = type.GetProperty("isLocked", BindingFlags.NonPublic | BindingFlags.Public |BindingFlags.Instance); | |
bool value = (bool)propertyInfo.GetValue(windowToBeLocked, null); | |
propertyInfo.SetValue(windowToBeLocked, !value, null); | |
windowToBeLocked.Repaint(); | |
} | |
else if (windowToBeLocked != null && windowToBeLocked.GetType().Name == "SceneHierarchyWindow") | |
{ | |
Type type = Assembly.GetAssembly(typeof(UnityEditor.Editor)).GetType("UnityEditor.SceneHierarchyWindow"); | |
FieldInfo fieldInfo = type.GetField("m_SceneHierarchy", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); | |
PropertyInfo propertyInfo = fieldInfo.FieldType.GetProperty("isLocked", | |
BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); | |
object value = fieldInfo.GetValue(windowToBeLocked); | |
bool value2 = (bool)propertyInfo.GetValue(value); | |
propertyInfo.SetValue(value, !value2, null); | |
windowToBeLocked.Repaint(); | |
} | |
} | |
} | |
} |
Brilliant! Still works in 2019.4.15
This is one of those things that is so useful that is blows my mind its a custom script. THANK YOU FOR MAKING IT AVAILABLE!!!!!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Alt + Q