Skip to content

Instantly share code, notes, and snippets.

@jfranmora
Last active August 24, 2021 20:26
Show Gist options
  • Save jfranmora/48b76dd300f78c6bfe1df76daf4eca9c to your computer and use it in GitHub Desktop.
Save jfranmora/48b76dd300f78c6bfe1df76daf4eca9c to your computer and use it in GitHub Desktop.
Unity utility to make components not editable in Editor.
using UnityEditor;
using UnityEngine;
public static class LockComponentEditor
{
[MenuItem("CONTEXT/Component/Lock Component")]
public static void LockComponent(MenuCommand command)
{
Component component = (Component)command.context;
component.hideFlags |= HideFlags.NotEditable;
EditorUtility.SetDirty(component);
}
[MenuItem("CONTEXT/Component/Lock Component", true)]
public static bool LockComponentValidate(MenuCommand command)
{
return command.context is Component component &&
(component.hideFlags & HideFlags.NotEditable) == 0;
}
[MenuItem("CONTEXT/Component/Unlock Component")]
public static void UnlockComponent(MenuCommand command)
{
Component component = (Component)command.context;
component.hideFlags &= (~HideFlags.NotEditable);
EditorUtility.SetDirty(component);
}
[MenuItem("CONTEXT/Component/Unlock Component", true)]
public static bool UnlockComponentValidate(MenuCommand command)
{
return command.context is Component component &&
(component.hideFlags & HideFlags.NotEditable) != 0;
}
}
@jfranmora
Copy link
Author

Make sure you place it in a Editor folder, otherwise the project will have compilation issues!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment