Created
June 29, 2017 14:06
-
-
Save elringus/2bed6d9fde41c7de43f57290bc7cd140 to your computer and use it in GitHub Desktop.
ReadOnly property drawer for Unity: add a [ReadOnly] attribute to a serialized field to make it show up as read only in the inspector
This file contains 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
// Put this outside of an 'Editor' folder. | |
using UnityEngine; | |
public class ReadOnlyAttribute : PropertyAttribute { } |
This file contains 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
// Put this to an 'Editor' folder. | |
using UnityEngine; | |
using UnityEditor; | |
[CustomPropertyDrawer(typeof(ReadOnlyAttribute))] | |
public class ReadOnlyPropertyDrawer : PropertyDrawer | |
{ | |
public override void OnGUI (Rect position, SerializedProperty property, GUIContent label) | |
{ | |
GUI.enabled = false; | |
EditorGUI.PropertyField(position, property, label, true); | |
GUI.enabled = true; | |
} | |
public override float GetPropertyHeight (SerializedProperty property, GUIContent label) | |
{ | |
return EditorGUI.GetPropertyHeight(property, label, true); | |
} | |
} |
As @Yolo-Jerome wrote, it's not perfect.
However, for all other cases, it is very useful and I thank you for it! :)
Best regards.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Unfortunately it doesn't fully work for Lists:
The most "dangerous" value to change is till available.
And it especially doesn't work for nested elements:
where the elements are of type
also if it is not in a list like