Here's a complete implementation of an Optional generic class for Unity that meets your requirements:
using System;
using UnityEngine;
[Serializable]
public class Optional<TValue, TSO> where TSO : ScriptableObject, IValueProvider<TValue>
{
public enum Mode
{
Disabled,
LocalValue,
GlobalValue
}
[SerializeField] private Mode mode = Mode.Disabled;
[SerializeField] private TValue localValue;
[SerializeField] private TSO globalValue;
public bool Enabled => mode != Mode.Disabled;
public Mode CurrentMode => mode;
public TValue Value
{
get
{
switch (mode)
{
case Mode.LocalValue:
return localValue;
case Mode.GlobalValue:
return globalValue != null ? globalValue.GetValue() : default(TValue);
default:
return default(TValue);
}
}
set
{
if (mode == Mode.LocalValue)
{
localValue = value;
}
}
}
public Optional() { }
public Optional(TValue value)
{
mode = Mode.LocalValue;
localValue = value;
}
public Optional(TSO scriptableObject)
{
mode = Mode.GlobalValue;
globalValue = scriptableObject;
}
public static implicit operator TValue(Optional<TValue, TSO> optional)
{
return optional.Value;
}
public static implicit operator bool(Optional<TValue, TSO> optional)
{
return optional.Enabled;
}
}
// Interface for ScriptableObjects that provide values
public interface IValueProvider<T>
{
T GetValue();
}
Now, here's the custom property drawer for the Inspector UI:
using UnityEngine;
using UnityEditor;
[CustomPropertyDrawer(typeof(Optional<,>))]
public class OptionalPropertyDrawer : PropertyDrawer
{
private const float dropdownHeight = 18f;
private const float spacing = 2f;
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(position, label, property);
// Get properties
SerializedProperty modeProperty = property.FindPropertyRelative("mode");
SerializedProperty localValueProperty = property.FindPropertyRelative("localValue");
SerializedProperty globalValueProperty = property.FindPropertyRelative("globalValue");
// Calculate rects
Rect labelRect = new Rect(position.x, position.y, EditorGUIUtility.labelWidth, dropdownHeight);
Rect dropdownRect = new Rect(position.x + EditorGUIUtility.labelWidth, position.y,
position.width - EditorGUIUtility.labelWidth, dropdownHeight);
// Draw label
EditorGUI.LabelField(labelRect, label);
// Draw dropdown
EditorGUI.PropertyField(dropdownRect, modeProperty, GUIContent.none);
// Draw value field based on mode
int mode = modeProperty.enumValueIndex;
if (mode != 0) // Not Disabled
{
Rect valueRect = new Rect(position.x, position.y + dropdownHeight + spacing,
position.width, EditorGUI.GetPropertyHeight(localValueProperty));
EditorGUI.indentLevel++;
if (mode == 1) // LocalValue
{
EditorGUI.PropertyField(valueRect, localValueProperty, new GUIContent("Value"));
}
else if (mode == 2) // GlobalValue
{
EditorGUI.PropertyField(valueRect, globalValueProperty, new GUIContent("Scriptable Object"));
}
EditorGUI.indentLevel--;
}
EditorGUI.EndProperty();
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
SerializedProperty modeProperty = property.FindPropertyRelative("mode");
int mode = modeProperty.enumValueIndex;
float height = dropdownHeight;
if (mode == 1) // LocalValue
{
SerializedProperty localValueProperty = property.FindPropertyRelative("localValue");
height += spacing + EditorGUI.GetPropertyHeight(localValueProperty);
}
else if (mode == 2) // GlobalValue
{
height += spacing + EditorGUIUtility.singleLineHeight;
}
return height;
}
}
Here's an example of how to create a ScriptableObject that implements the interface:
using UnityEngine;
[CreateAssetMenu(fileName = "FloatValue", menuName = "Values/Float Value")]
public class FloatValue : ScriptableObject, IValueProvider<float>
{
[SerializeField] private float value;
public float GetValue() => value;
public void SetValue(float newValue) => value = newValue;
}
[CreateAssetMenu(fileName = "IntValue", menuName = "Values/Int Value")]
public class IntValue : ScriptableObject, IValueProvider<int>
{
[SerializeField] private int value;
public int GetValue() => value;
public void SetValue(int newValue) => value = newValue;
}
And here's an example of how to use the Optional class in a MonoBehaviour:
using UnityEngine;
public class ExampleUsage : MonoBehaviour
{
[SerializeField] private Optional<float, FloatValue> optionalFloat;
[SerializeField] private Optional<int, IntValue> optionalInt;
void Start()
{
// Check if enabled
if (optionalFloat.Enabled)
{
// Get value (automatically converts based on mode)
float value = optionalFloat.Value;
Debug.Log($"Float value: {value}");
}
// Implicit conversion to value type
float implicitValue = optionalFloat;
// Implicit conversion to bool (checks if enabled)
if (optionalFloat)
{
Debug.Log("Optional float is enabled");
}
}
}
This implementation provides:
-
Generic value type and ScriptableObject support through the Optional<TValue, TSO> class with the constraint that TSO must implement IValueProvider<TValue>
-
Dropdown menu with three options (Disabled, LocalValue, GlobalValue) that controls what's displayed in the inspector and sets the Enabled state
-
Value conversion logic through the Value property that returns the appropriate value based on the current mode, plus implicit operators for convenience
-
Unity Inspector UI through the custom property drawer that shows/hides fields based on the selected mode
The system is flexible and can be extended with additional ScriptableObject types by creating new classes that implement IValueProvider<T> for different value types.