ScriptableObjectVariable Drawers

Usage:

Code (CSharp):

public class SomeMonoBehaviour : MonoBehaviour { public IntReference test1; public FloatReference test2; }

Odin Drawer:

Code (CSharp):

[OdinDrawer] public class VariableReferenceDrawer : OdinValueDrawer where TReference : VariableReference where TVariable : ScriptableObjectVariable { protected override void DrawPropertyLayout(IPropertyValueEntry entry, GUIContent label) { var value = entry.SmartValue; GUILayout.BeginVertical; { var btnRect = GUIHelper.GetCurrentLayoutRect; btnRect.width = EditorGUIUtility.labelWidth; btnRect = btnRect.AlignRight(18); btnRect.y += 4; if (GUI.Button(btnRect, GUIContent.none, "PaneOptions")) { var menu = new GenericMenu; menu.AddItem(new GUIContent("Constant"), value.UseConstant, => value.UseConstant = true); menu.AddItem(new GUIContent("Variable"), !value.UseConstant, => value.UseConstant = false); menu.ShowAsContext; } EditorGUIUtility.AddCursorRect(btnRect, MouseCursor.Arrow); if (value.UseConstant) { entry.Property.Children["ConstantValue"].Draw(label); } else { entry.Property.Children["Variable"].Draw(label); } } GUILayout.EndVertical; } }

Variable and reference definitions:

Code (CSharp):

[CreateAssetMenu(fileName = "FloatVariable", menuName = "Scriptable Object Variables/Float")] public class FloatVariable : ScriptableObjectVariable { } [Serializable] public class FloatReference : VariableReference { } [CreateAssetMenu(fileName = "IntVariable", menuName = "Scriptable Object Variables/Int")] public class IntVariable : ScriptableObjectVariable { } [Serializable] public class IntReference : VariableReference { }

Base classes:

Code (CSharp):

public abstract class ScriptableObjectVariable : ScriptableObject { [LabelText("$name")] public T Value; } public abstract class VariableReference where TVariable : ScriptableObjectVariable { public bool UseConstant; public TValue ConstantValue; [SuffixLabel("$GetValueString", true)] public TVariable Variable; public TValue Value { get { return !this.UseConstant && this.Variable ? this.Variable.Value : this.ConstantValue; } } private string GetValueString { return this.ConstantValue.ToString + "   "; } }