Created
August 6, 2021 14:33
-
-
Save made-indrayana/1134c958c7e694be792f6be2d38f6b64 to your computer and use it in GitHub Desktop.
Read Only attribute for Unity's Inspector
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
// 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 { } |
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
// 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