Last active
February 19, 2024 19:12
-
-
Save u-m-i/cf134a9c143af5780006c2000674b07f to your computer and use it in GitHub Desktop.
The all mighty ReadOnlyAttribute
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 UnityEngine; | |
///<summary> The ReadOnly attribute for fields, allows inheritance and have its own custom drawer </summary> | |
public class ReadOnlyAttribute : PropertyAttribute | |
{} |
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
// ----------------- | |
// Resources | |
// https://docs.unity3d.com/ScriptReference/MessageType.html | |
// https://discussions.unity.com/t/how-to-make-a-readonly-property-in-inspector/75448/7 | |
// https://docs.unity3d.com/ScriptReference/GUI-enabled.html | |
// ----------------- | |
using UnityEditor; | |
using UnityEngine; | |
[CustomPropertyDrawer(typeof(ReadOnlyAttribute))] | |
public class ReadOnlyDrawer : PropertyDrawer | |
{ | |
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) | |
{ | |
GUI.enabled = false; | |
EditorGUI.PropertyField(position, property, label, true); // Include children could be the attribute's property | |
GUI.enabled = true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment