暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

InputControlPathDrawer.cs 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #if UNITY_EDITOR
  2. using System;
  3. using UnityEditor;
  4. using UnityEngine.InputSystem.Layouts;
  5. namespace UnityEngine.InputSystem.Editor
  6. {
  7. /// <summary>
  8. /// Custom property drawer for string type fields that represent input control paths.
  9. /// </summary>
  10. /// <remarks>
  11. /// To use this drawer on a property, apply <see cref="InputControlAttribute"/> on it. To constrain
  12. /// what type of control is picked, set the <see cref="InputControlAttribute.layout"/> field on the
  13. /// attribute.
  14. ///
  15. /// <example>
  16. /// <code>
  17. /// // A string representing a control path. Constrain it to picking Button-type controls.
  18. /// [InputControl(layout = "Button")]
  19. /// [SerializeField] private string m_ControlPath;
  20. /// </code>
  21. /// </example>
  22. /// </remarks>
  23. [CustomPropertyDrawer(typeof(InputControlAttribute))]
  24. internal sealed class InputControlPathDrawer : PropertyDrawer, IDisposable
  25. {
  26. private InputControlPickerState m_PickerState;
  27. private InputControlPathEditor m_Editor;
  28. public void Dispose()
  29. {
  30. m_Editor?.Dispose();
  31. }
  32. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  33. {
  34. if (m_PickerState == null)
  35. m_PickerState = new InputControlPickerState();
  36. if (m_Editor == null)
  37. {
  38. m_Editor = new InputControlPathEditor(property, m_PickerState,
  39. () => property.serializedObject.ApplyModifiedProperties(),
  40. label: label);
  41. }
  42. EditorGUI.BeginProperty(position, label, property);
  43. m_Editor.OnGUI(position, label, property, () => property.serializedObject.ApplyModifiedProperties());
  44. EditorGUI.EndProperty();
  45. }
  46. }
  47. }
  48. #endif // UNITY_EDITOR