Here's a Unity C# implementation for an Optional generic class with the features you requested:
using UnityEngine;
using UnityEditor;
using System;
// Interface for ScriptableObjects that provide values
public interface IValueProvider<T>
{
T GetValue();
}
// Enum for selection mode
public enum OptionalMode
{
Disabled,
LocalValue,
GlobalValue
}
// The Optional class
[Serializable]
public class Optional<T, TSO> where TSO : ScriptableObject, IValueProvider<T>
{
[SerializeField] private OptionalMode _mode = OptionalMode.Disabled;
[SerializeField] private T _localValue;
[SerializeField] private TSO _globalValue;
public bool Enabled => _mode != OptionalMode.Disabled;
public OptionalMode Mode => _mode;
public T LocalValue => _localValue;
public TSO GlobalValue => _globalValue;
// Get the current value based on the mode
public T GetValue()
{
switch (_mode)
{
case OptionalMode.LocalValue:
return _localValue;
case OptionalMode.GlobalValue:
return _globalValue != null ? _globalValue.GetValue() : default;
default:
return default;
}
}
// Set the local value
public void SetLocalValue(T value)
{
_localValue = value;
}
// Set the global value reference
public void SetGlobalValue(TSO value)
{
_globalValue = value;
}
// Set the mode
public void SetMode(OptionalMode mode)
{
_mode = mode;
}
}
#if UNITY_EDITOR
// Custom property drawer for the Optional class
[CustomPropertyDrawer(typeof(Optional<,>))]
public class OptionalDrawer : PropertyDrawer
{
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
var modeProperty = property.FindPropertyRelative("_mode");
var mode = (OptionalMode)modeProperty.enumValueIndex;
// Base height for the mode dropdown
float height = EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
// Add height for the value field based on mode
if (mode == OptionalMode.LocalValue)
{
var valueProperty = property.FindPropertyRelative("_localValue");
height += EditorGUI.GetPropertyHeight(valueProperty, true) + EditorGUIUtility.standardVerticalSpacing;
}
else if (mode == OptionalMode.GlobalValue)
{
var soProperty = property.FindPropertyRelative("_globalValue");
height += EditorGUI.GetPropertyHeight(soProperty, true) + EditorGUIUtility.standardVerticalSpacing;
}
return height;
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(position, label, property);
// Draw label
var contentRect = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
// Calculate rects
var modeRect = new Rect(contentRect.x, contentRect.y, contentRect.width, EditorGUIUtility.singleLineHeight);
// Get properties
var modeProperty = property.FindPropertyRelative("_mode");
var localValueProperty = property.FindPropertyRelative("_localValue");
var globalValueProperty = property.FindPropertyRelative("_globalValue");
// Draw mode dropdown
EditorGUI.PropertyField(modeRect, modeProperty, GUIContent.none);
// Draw appropriate value field based on mode
var mode = (OptionalMode)modeProperty.enumValueIndex;
if (mode == OptionalMode.LocalValue)
{
var valueRect = new Rect(
contentRect.x,
modeRect.y + EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing,
contentRect.width,
EditorGUI.GetPropertyHeight(localValueProperty)
);
EditorGUI.PropertyField(valueRect, localValueProperty, new GUIContent("Value"), true);
}
else if (mode == OptionalMode.GlobalValue)
{
var soRect = new Rect(
contentRect.x,
modeRect.y + EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing,
contentRect.width,
EditorGUI.GetPropertyHeight(globalValueProperty)
);
EditorGUI.PropertyField(soRect, globalValueProperty, new GUIContent("Value SO"), true);
}
EditorGUI.EndProperty();
}
}
#endif
// Example usage:
// Create a ScriptableObject that implements IValueProvider
[CreateAssetMenu(fileName = "FloatValue", menuName = "ScriptableObjects/FloatValue")]
public class FloatValueSO : ScriptableObject, IValueProvider<float>
{
[SerializeField] private float _value;
public float GetValue()
{
return _value;
}
}
// Example MonoBehaviour using the Optional class
public class OptionalExample : MonoBehaviour
{
[SerializeField] private Optional<float, FloatValueSO> _optionalFloat;
void Start()
{
// Get the current value
float value = _optionalFloat.GetValue();
Debug.Log($"Optional value: {value}");
}
}
Would you like me to explain or break down the code?