-
-
Save unitycoder/d0c1c99b181f470cc3bb8b97dbc173ac to your computer and use it in GitHub Desktop.
Lock Inspector Icon and Transform Constrain Proportion Icon in Unity
This file contains hidden or 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.Reflection; | |
using UnityEditor; | |
using UnityEngine; | |
/// <summary> | |
/// Toggles the Inspector lock state and the Constrain Proportions lock state. | |
/// </summary> | |
public static class LockInspector { | |
static readonly MethodInfo flipLocked; | |
static readonly PropertyInfo constrainProportions; | |
const BindingFlags bindingFlags = BindingFlags.NonPublic | BindingFlags.Instance; | |
static LockInspector() { | |
// Cache static MethodInfo and PropertyInfo for performance | |
#if UNITY_2023_2_OR_NEWER | |
var editorLockTrackerType = typeof(EditorGUIUtility).Assembly.GetType("UnityEditor.EditorGUIUtility+EditorLockTracker"); | |
flipLocked = editorLockTrackerType.GetMethod("FlipLocked", bindingFlags); | |
#endif | |
constrainProportions = typeof(Transform).GetProperty("constrainProportionsScale", bindingFlags); | |
} | |
[MenuItem("Edit/Toggle Inspector Lock %l")] | |
public static void Lock() { | |
#if UNITY_2023_2_OR_NEWER | |
// New approach for Unity 2023.2 and above, including Unity 6 | |
var inspectorWindowType = typeof(Editor).Assembly.GetType("UnityEditor.InspectorWindow"); | |
foreach (var inspectorWindow in Resources.FindObjectsOfTypeAll(inspectorWindowType)) { | |
var lockTracker = inspectorWindowType.GetField("m_LockTracker", bindingFlags)?.GetValue(inspectorWindow); | |
flipLocked?.Invoke(lockTracker, new object[] { }); | |
} | |
#else | |
// Old approach for Unity versions before 2023.2 | |
ActiveEditorTracker.sharedTracker.isLocked = !ActiveEditorTracker.sharedTracker.isLocked; | |
#endif | |
// Constrain Proportions lock for all versions including Unity 6 | |
foreach (var activeEditor in ActiveEditorTracker.sharedTracker.activeEditors) { | |
if (activeEditor.target is not Transform target) continue; | |
var currentValue = (bool) constrainProportions.GetValue(target, null); | |
constrainProportions.SetValue(target, !currentValue, null); | |
} | |
ActiveEditorTracker.sharedTracker.ForceRebuild(); | |
} | |
[MenuItem("Edit/Toggle Inspector Lock %l", true)] | |
public static bool Valid() { | |
return ActiveEditorTracker.sharedTracker.activeEditors.Length != 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment