unity-atoms/Source/Editor/ReferenceDrawer.cs
2019-04-08 16:14:50 +02:00

78 lines
2.9 KiB
C#

using UnityEditor;
using UnityEngine;
namespace UnityAtoms
{
public abstract class ReferenceDrawer : PropertyDrawer
{
private static readonly string[] _popupOptions =
{ "Use Constant", "Use Variable" };
private static GUIStyle _popupStyle;
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
if (_popupStyle == null)
{
_popupStyle = new GUIStyle(GUI.skin.GetStyle("PaneOptions"));
_popupStyle.imagePosition = ImagePosition.ImageOnly;
}
label = EditorGUI.BeginProperty(position, label, property);
position = EditorGUI.PrefixLabel(position, label);
EditorGUI.BeginChangeCheck();
// Get properties
SerializedProperty useConstant = property.FindPropertyRelative("UseConstant");
SerializedProperty constantValue = property.FindPropertyRelative("ConstantValue");
SerializedProperty variable = property.FindPropertyRelative("Variable");
// Calculate rect for configuration button
Rect buttonRect = new Rect(position);
buttonRect.yMin += _popupStyle.margin.top;
buttonRect.width = _popupStyle.fixedWidth + _popupStyle.margin.right;
position.xMin = buttonRect.xMax;
// Store old indent level and set it to 0, the PrefixLabel takes care of it
int indent = EditorGUI.indentLevel;
EditorGUI.indentLevel = 0;
int result = EditorGUI.Popup(buttonRect, useConstant.boolValue ? 0 : 1, _popupOptions, _popupStyle);
useConstant.boolValue = result == 0;
EditorGUI.PropertyField(position,
useConstant.boolValue ? constantValue : variable,
GUIContent.none);
if (EditorGUI.EndChangeCheck())
property.serializedObject.ApplyModifiedProperties();
EditorGUI.indentLevel = indent;
EditorGUI.EndProperty();
}
[CustomPropertyDrawer(typeof(BoolReference))]
public sealed class BoolReferenceDrawer : ReferenceDrawer { }
[CustomPropertyDrawer(typeof(ColorReference))]
public sealed class ColorReferenceDrawer : ReferenceDrawer { }
[CustomPropertyDrawer(typeof(FloatReference))]
public sealed class FloatReferenceDrawer : ReferenceDrawer { }
[CustomPropertyDrawer(typeof(IntReference))]
public sealed class IntReferenceDrawer : ReferenceDrawer { }
[CustomPropertyDrawer(typeof(StringReference))]
public sealed class StringReferenceDrawer : ReferenceDrawer { }
[CustomPropertyDrawer(typeof(Vector2Reference))]
public sealed class Vector2ReferenceDrawer : ReferenceDrawer { }
[CustomPropertyDrawer(typeof(Vector3Reference))]
public sealed class Vector3ReferenceDrawer : ReferenceDrawer { }
}
}