Skip to content

Instantly share code, notes, and snippets.

@u-m-i
Last active February 19, 2024 19:12
Show Gist options
  • Save u-m-i/cf134a9c143af5780006c2000674b07f to your computer and use it in GitHub Desktop.
Save u-m-i/cf134a9c143af5780006c2000674b07f to your computer and use it in GitHub Desktop.
The all mighty ReadOnlyAttribute
using UnityEngine;
///<summary> The ReadOnly attribute for fields, allows inheritance and have its own custom drawer </summary>
public class ReadOnlyAttribute : PropertyAttribute
{}
// -----------------
// 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