Instantly share code, notes, and snippets.

Embed
What would you like to do?
Read Only Attribute for Unity (just mark stuff as [ReadOnly] the same way you would use [HideInInspector])
using UnityEngine;
using System;
using System.Reflection;
using System.Text.RegularExpressions;
[AttributeUsage (AttributeTargets.Field,Inherited = true)]
public class ReadOnlyAttribute : PropertyAttribute {}
#if UNITY_EDITOR
[UnityEditor.CustomPropertyDrawer (typeof(ReadOnlyAttribute))]
public class ReadOnlyAttributeDrawer : UnityEditor.PropertyDrawer
{
public override void OnGUI(Rect rect, UnityEditor.SerializedProperty prop, GUIContent label)
{
bool wasEnabled = GUI.enabled;
GUI.enabled = false;
UnityEditor.EditorGUI.PropertyField(rect,prop);
GUI.enabled = wasEnabled;
}
}
#endif
@Flavelius

This comment has been minimized.

Flavelius commented May 3, 2018

UnityEditor.EditorGUI.PropertyField(rect,prop, true); //to include children

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