Last active
September 23, 2023 16:34
-
-
Save MattRix/9fb45606bfbc16254641e4d462117737 to your computer and use it in GitHub Desktop.
Read Only Attribute for Unity (just mark stuff as [ReadOnly] the same way you would use [HideInInspector])
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; | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A few years later but I solved this issue by replacing
with: