Skip to content

Instantly share code, notes, and snippets.

@CapitanLiteral
Last active February 24, 2022 19:45
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CapitanLiteral/0472d5e9f8e21dcbae6f2a81e2c6dbaf to your computer and use it in GitHub Desktop.
Save CapitanLiteral/0472d5e9f8e21dcbae6f2a81e2c6dbaf to your computer and use it in GitHub Desktop.
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();
}
}
}
}
@CapitanLiteral
Copy link
Author

Alt + Q

@ChameleonSloth
Copy link

Brilliant! Still works in 2019.4.15

@Modern-Epic
Copy link

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