Skip to content

Instantly share code, notes, and snippets.

@samizzo
Created December 12, 2018 06:16
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samizzo/9a691209cf2c997523fd8ba18db3579e to your computer and use it in GitHub Desktop.
Save samizzo/9a691209cf2c997523fd8ba18db3579e to your computer and use it in GitHub Desktop.
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class ReadOnlyAttribute : PropertyAttribute
{
}
#if UNITY_EDITOR
[CustomPropertyDrawer(typeof(ReadOnlyAttribute))]
public class ReadOnlyFieldDrawer : PropertyDrawer
{
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return EditorGUI.GetPropertyHeight(property, label, true);
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
var guiEnabled = GUI.enabled;
GUI.enabled = false;
EditorGUI.PropertyField(position, property, label, true);
GUI.enabled = guiEnabled;
}
}
#endif
@samizzo
Copy link
Author

samizzo commented Dec 12, 2018

Usage example:

[SerializeField]
[ReadOnly]
private float m_somePrivateFloat;

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