Last active
August 24, 2021 20:26
-
-
Save jfranmora/48b76dd300f78c6bfe1df76daf4eca9c to your computer and use it in GitHub Desktop.
Unity utility to make components not editable in Editor.
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 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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Make sure you place it in a Editor folder, otherwise the project will have compilation issues!