InputActionProperty.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using System;
  2. namespace UnityEngine.InputSystem
  3. {
  4. /// <summary>
  5. /// A serializable property type that can either reference an action externally defined
  6. /// in an <see cref="InputActionAsset"/> or define a new action directly on the property.
  7. /// </summary>
  8. /// <remarks>
  9. /// This struct is meant to be used for serialized fields in <c>MonoBehaviour</c> and
  10. /// <c>ScriptableObject</c> classes. It has a custom property drawer attached to it
  11. /// that allows to switch between using the property as a reference and using it
  12. /// to define an action in place.
  13. ///
  14. /// <example>
  15. /// <code>
  16. /// public class MyBehavior : MonoBehaviour
  17. /// {
  18. /// // This can be edited in the inspector to either reference an existing
  19. /// // action or to define an action directly on the component.
  20. /// public InputActionProperty myAction;
  21. /// }
  22. /// </code>
  23. /// </example>
  24. /// </remarks>
  25. /// <seealso cref="InputAction"/>
  26. /// <seealso cref="InputActionReference"/>
  27. [Serializable]
  28. public struct InputActionProperty : IEquatable<InputActionProperty>, IEquatable<InputAction>, IEquatable<InputActionReference>
  29. {
  30. /// <summary>
  31. /// The action held on to by the property.
  32. /// </summary>
  33. /// <value>The action object contained in the property.</value>
  34. /// <remarks>
  35. /// This property will return <c>null</c> if the property using a <see cref="reference"/> and
  36. /// the referenced action cannot be found. Also, it will be <c>null</c> if the property
  37. /// has been manually initialized with a <c>null</c> <see cref="InputAction"/> using
  38. /// <see cref="InputActionProperty(InputAction)"/>.
  39. /// </remarks>
  40. public InputAction action => m_UseReference ? m_Reference.action : m_Action;
  41. /// <summary>
  42. /// If the property contains a reference to the action, this property returns
  43. /// the reference. Otherwise it returns <c>null</c>.
  44. /// </summary>
  45. /// <value>Reference to external input action, if defined.</value>
  46. public InputActionReference reference => m_UseReference ? m_Reference : null;
  47. /// <summary>
  48. /// Initialize the property to contain the given action.
  49. /// </summary>
  50. /// <param name="action">An action.</param>
  51. /// <remarks>
  52. /// When the struct is serialized, it will serialize the given action as part of it.
  53. /// The <see cref="reference"/> property will return <c>null</c>.
  54. /// </remarks>
  55. public InputActionProperty(InputAction action)
  56. {
  57. m_UseReference = false;
  58. m_Action = action;
  59. m_Reference = null;
  60. }
  61. /// <summary>
  62. /// Initialize the property to use the given action reference.
  63. /// </summary>
  64. /// <param name="reference">Reference to an <see cref="InputAction"/>.</param>
  65. /// <remarks>
  66. /// When the struct is serialized, it will only serialize a reference to
  67. /// the given <paramref name="reference"/> object.
  68. /// </remarks>
  69. public InputActionProperty(InputActionReference reference)
  70. {
  71. m_UseReference = true;
  72. m_Action = null;
  73. m_Reference = reference;
  74. }
  75. /// <summary>
  76. /// Compare two action properties to see whether they refer to the same action.
  77. /// </summary>
  78. /// <param name="other">Another action property.</param>
  79. /// <returns>True if both properties refer to the same action.</returns>
  80. public bool Equals(InputActionProperty other)
  81. {
  82. return m_Reference == other.m_Reference &&
  83. m_UseReference == other.m_UseReference &&
  84. m_Action == other.m_Action;
  85. }
  86. /// <summary>
  87. /// Check whether the property refers to the same action.
  88. /// </summary>
  89. /// <param name="other">An action.</param>
  90. /// <returns>True if <see cref="action"/> is the same as <paramref name="other"/>.</returns>
  91. public bool Equals(InputAction other)
  92. {
  93. return ReferenceEquals(action, other);
  94. }
  95. /// <summary>
  96. /// Check whether the property references the same action.
  97. /// </summary>
  98. /// <param name="other">An action reference.</param>
  99. /// <returns>True if the property and <paramref name="other"/> reference the same action.</returns>
  100. public bool Equals(InputActionReference other)
  101. {
  102. return m_Reference == other;
  103. }
  104. /// <summary>
  105. /// Check whether the given object is an InputActionProperty referencing the same action.
  106. /// </summary>
  107. /// <param name="obj">An object or <c>null</c>.</param>
  108. /// <returns>True if the given <paramref name="obj"/> is an InputActionProperty equivalent to this one.</returns>
  109. /// <seealso cref="Equals(InputActionProperty)"/>
  110. public override bool Equals(object obj)
  111. {
  112. if (m_UseReference)
  113. return Equals(obj as InputActionReference);
  114. return Equals(obj as InputAction);
  115. }
  116. /// <summary>
  117. /// Compute a hash code for the object.
  118. /// </summary>
  119. /// <returns>A hash code.</returns>
  120. public override int GetHashCode()
  121. {
  122. if (m_UseReference)
  123. return m_Reference.GetHashCode();
  124. return m_Action.GetHashCode();
  125. }
  126. /// <summary>
  127. /// Compare the two properties for equivalence.
  128. /// </summary>
  129. /// <param name="left">The first property.</param>
  130. /// <param name="right">The second property.</param>
  131. /// <returns>True if the two action properties are equivalent.</returns>
  132. /// <seealso cref="Equals(InputActionProperty)"/>
  133. public static bool operator==(InputActionProperty left, InputActionProperty right)
  134. {
  135. return left.Equals(right);
  136. }
  137. /// <summary>
  138. /// Compare the two properties for not being equivalent.
  139. /// </summary>
  140. /// <param name="left">The first property.</param>
  141. /// <param name="right">The second property.</param>
  142. /// <returns>True if the two action properties are not equivalent.</returns>
  143. /// <seealso cref="Equals(InputActionProperty)"/>
  144. public static bool operator!=(InputActionProperty left, InputActionProperty right)
  145. {
  146. return !left.Equals(right);
  147. }
  148. [SerializeField] private bool m_UseReference;
  149. [SerializeField] private InputAction m_Action;
  150. [SerializeField] private InputActionReference m_Reference;
  151. }
  152. }