File size: 4,011 Bytes
18a519f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | #if UNITY_EDITOR
using UnityEditor;
namespace UnityEngine.InputSystem.Editor
{
/// <summary>
/// A custom property drawer for <see cref="InputActionProperty"/>.
/// </summary>
/// <remarks>
/// This is basically a toggle between the editor for <see cref="InputActionReference"/>
/// and the editor for <see cref="InputAction"/>.
/// </remarks>
[CustomPropertyDrawer(typeof(InputActionProperty))]
internal class InputActionPropertyDrawer : PropertyDrawer
{
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
if (property == null)
throw new System.ArgumentNullException(nameof(property));
var height = 0f;
// Field label.
height += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
// "Use Reference" toggle.
height += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
// We show either the InputAction property drawer or InputAssetReferenceDrawer.
var useReference = property.FindPropertyRelative("m_UseReference");
if (!useReference.boolValue)
{
var actionProperty = property.FindPropertyRelative("m_Action");
height += EditorGUI.GetPropertyHeight(actionProperty);
}
else
{
var referenceProperty = property.FindPropertyRelative("m_Reference");
height += EditorGUI.GetPropertyHeight(referenceProperty);
}
return height;
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
if (property == null)
throw new System.ArgumentNullException(nameof(property));
EditorGUI.BeginProperty(position, label, property);
const int kIndent = 16;
var titleRect = position;
titleRect.height = EditorGUIUtility.singleLineHeight;
EditorGUI.LabelField(titleRect, label, Styles.header);
var useReferenceToggleRect = position;
useReferenceToggleRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
useReferenceToggleRect.height = EditorGUIUtility.singleLineHeight;
useReferenceToggleRect.x += kIndent;
useReferenceToggleRect.width -= kIndent;
var useReference = property.FindPropertyRelative("m_UseReference");
EditorGUI.PropertyField(useReferenceToggleRect, useReference);
if (!useReference.boolValue)
{
var actionProperty = property.FindPropertyRelative("m_Action");
var actionDrawerRect = position;
actionDrawerRect.x += kIndent;
actionDrawerRect.width -= kIndent;
actionDrawerRect.y += (useReferenceToggleRect.height + EditorGUIUtility.standardVerticalSpacing) * 2;
actionDrawerRect.height = EditorGUI.GetPropertyHeight(actionProperty);
EditorGUI.PropertyField(actionDrawerRect, actionProperty);
}
else
{
var referenceProperty = property.FindPropertyRelative("m_Reference");
var referenceRect = position;
referenceRect.x += kIndent;
referenceRect.width -= kIndent;
referenceRect.y += (useReferenceToggleRect.height + EditorGUIUtility.standardVerticalSpacing) * 2;
referenceRect.height = EditorGUI.GetPropertyHeight(referenceProperty);
EditorGUI.PropertyField(referenceRect, referenceProperty);
}
EditorGUI.EndProperty();
}
private static class Styles
{
public static readonly GUIStyle header = new GUIStyle("Label").WithFontStyle(FontStyle.Bold);
}
}
}
#endif // UNITY_EDITOR
|