WinterJamSnowman/Assets/Editor/OptionalPropertyDrawer.cs

40 lines
1.3 KiB
C#
Raw Permalink Normal View History

2023-01-24 13:51:46 +00:00
using UnityEditor;
using UnityEngine;
using Lemon.GenericLib.Generics;
namespace Lemon.GenericLib.Editor {
[CustomPropertyDrawer(typeof(Optional<>))]
public class OptionalPropertyDrawer : PropertyDrawer
{
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
var valueProperty = property.FindPropertyRelative("value");
return EditorGUI.GetPropertyHeight(valueProperty);
}
public override void OnGUI(
Rect position,
SerializedProperty property,
GUIContent label
)
{
var valueProperty = property.FindPropertyRelative("value");
var enabledProperty = property.FindPropertyRelative("enabled");
EditorGUI.BeginProperty(position, label, property);
position.width -= 24;
EditorGUI.BeginDisabledGroup(!enabledProperty.boolValue);
EditorGUI.PropertyField(position, valueProperty, label, true);
EditorGUI.EndDisabledGroup();
position.x += position.width + 24;
position.width = position.height = EditorGUI.GetPropertyHeight(enabledProperty);
position.x -= position.width;
EditorGUI.PropertyField(position, enabledProperty, GUIContent.none);
EditorGUI.EndProperty();
}
}
}