Skip to content

Instantly share code, notes, and snippets.

@aarthificial
Last active July 3, 2024 22:17
Show Gist options
  • Save aarthificial/f2dbb58e4dbafd0a93713a380b9612af to your computer and use it in GitHub Desktop.
Save aarthificial/f2dbb58e4dbafd0a93713a380b9612af to your computer and use it in GitHub Desktop.

Revisions

  1. aarthificial revised this gist Jun 27, 2021. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions OptionalPropertyDrawer.cs
    Original file line number Diff line number Diff line change
    @@ -27,10 +27,13 @@ GUIContent label
    EditorGUI.PropertyField(position, valueProperty, label, true);
    EditorGUI.EndDisabledGroup();

    int indent = EditorGUI.indentLevel;
    EditorGUI.indentLevel = 0;
    position.x += position.width + 24;
    position.width = position.height = EditorGUI.GetPropertyHeight(enabledProperty);
    position.x -= position.width;
    EditorGUI.PropertyField(position, enabledProperty, GUIContent.none);
    EditorGUI.indentLevel = indent;
    EditorGUI.EndProperty();
    }
    }
  2. aarthificial revised this gist May 17, 2021. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions Optional.cs
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,7 @@
    using UnityEngine;

    [Serializable]
    /// Requires Unity 2020.1+
    public struct Optional<T>
    {
    [SerializeField] private bool enabled;
  3. aarthificial revised this gist Mar 22, 2021. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions OptionalPropertyDrawer.cs
    Original file line number Diff line number Diff line change
    @@ -21,6 +21,7 @@ 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);
    @@ -30,6 +31,7 @@ GUIContent label
    position.width = position.height = EditorGUI.GetPropertyHeight(enabledProperty);
    position.x -= position.width;
    EditorGUI.PropertyField(position, enabledProperty, GUIContent.none);
    EditorGUI.EndProperty();
    }
    }
    }
  4. aarthificial created this gist Mar 14, 2021.
    18 changes: 18 additions & 0 deletions Optional.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    using System;
    using UnityEngine;

    [Serializable]
    public struct Optional<T>
    {
    [SerializeField] private bool enabled;
    [SerializeField] private T value;

    public bool Enabled => enabled;
    public T Value => value;

    public Optional(T initialValue)
    {
    enabled = true;
    value = initialValue;
    }
    }
    35 changes: 35 additions & 0 deletions OptionalPropertyDrawer.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    using UnityEditor;
    using UnityEngine;

    namespace 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");

    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);
    }
    }
    }