Ingen beskrivning
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.

InputActionDrawer.cs 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #if UNITY_EDITOR
  2. using System;
  3. using UnityEditor;
  4. using UnityEditor.IMGUI.Controls;
  5. namespace UnityEngine.InputSystem.Editor
  6. {
  7. /// <summary>
  8. /// Property drawer for <see cref="InputAction"/>.
  9. /// </summary>
  10. [CustomPropertyDrawer(typeof(InputAction))]
  11. internal class InputActionDrawer : InputActionDrawerBase
  12. {
  13. protected override TreeViewItem BuildTree(SerializedProperty property)
  14. {
  15. return InputActionTreeView.BuildWithJustBindingsFromAction(property);
  16. }
  17. protected override string GetSuffixToRemoveFromPropertyDisplayName()
  18. {
  19. return " Action";
  20. }
  21. protected override bool IsPropertyAClone(SerializedProperty property)
  22. {
  23. // When a new item is added to a collection through the inspector, the default behaviour is
  24. // to create a clone of the previous item. Here we look at all InputActions that appear before
  25. // the current one and compare their Ids to determine if we have a clone. We don't look past
  26. // the current item because Unity will be calling this property drawer for each input action
  27. // in the collection in turn. If the user just added a new input action, and it's a clone, as
  28. // we work our way down the list, we'd end up thinking that an existing input action was a clone
  29. // of the newly added one, instead of the other way around. If we do have a clone, we need to
  30. // clear out some properties of the InputAction (id, name, and singleton action bindings) and
  31. // recreate the tree view.
  32. if (property?.GetParentProperty() == null || property.GetParentProperty().isArray == false)
  33. return false;
  34. var array = property.GetArrayPropertyFromElement();
  35. var index = property.GetIndexOfArrayElement();
  36. for (var i = 0; i < index; i++)
  37. {
  38. if (property.FindPropertyRelative(nameof(InputAction.m_Id))?.stringValue ==
  39. array.GetArrayElementAtIndex(i)?.FindPropertyRelative(nameof(InputAction.m_Id))?.stringValue)
  40. return true;
  41. }
  42. return false;
  43. }
  44. protected override void ResetProperty(SerializedProperty property)
  45. {
  46. if (property == null) return;
  47. property.SetStringValue(nameof(InputAction.m_Id), Guid.NewGuid().ToString());
  48. property.SetStringValue(nameof(InputAction.m_Name), "Input Action");
  49. property.FindPropertyRelative(nameof(InputAction.m_SingletonActionBindings))?.ClearArray();
  50. property.serializedObject?.ApplyModifiedPropertiesWithoutUndo();
  51. }
  52. }
  53. }
  54. #endif // UNITY_EDITOR