Skip to content

Instantly share code, notes, and snippets.

@made-indrayana
Created August 6, 2021 14:33
Show Gist options
  • Save made-indrayana/1134c958c7e694be792f6be2d38f6b64 to your computer and use it in GitHub Desktop.
Save made-indrayana/1134c958c7e694be792f6be2d38f6b64 to your computer and use it in GitHub Desktop.
Read Only attribute for Unity's Inspector
// ReadOnlyAttribute.cs
// Author: Made Indrayana
// MIT License
// Variables with this attribute will be shown in Inspector but will be grayed out and not editable.
using UnityEngine;
public class ReadOnlyAttribute : PropertyAttribute { }
// ReadOnlyAttributeDrawer.cs
// Author: Made Indrayana
// MIT License
// Variables with this attribute will be shown in Inspector but will be grayed out and not editable.
using UnityEngine;
using UnityEditor;
[CustomPropertyDrawer(typeof(ReadOnlyAttribute))]
public class ReadOnlyAttributeDrawer : PropertyDrawer
{
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return EditorGUI.GetPropertyHeight(property, label, true);
}
// Draw a disabled property field
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
GUI.enabled = false;
EditorGUI.PropertyField(position, property, label, true);
GUI.enabled = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment