Açıklama Yok
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.

InputActionProperty.cs 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 effective action held on to by the property.
  32. /// </summary>
  33. /// <value>The effective action object contained in the property.</value>
  34. /// <remarks>
  35. /// This property will return <c>null</c> if the property is 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 != null ? m_Reference.action : null : m_Action;
  41. /// <summary>
  42. /// If the property is set to use 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. /// The serialized loose action created in code serialized with this property.
  49. /// </summary>
  50. /// <value>The serialized action field.</value>
  51. internal InputAction serializedAction => m_Action;
  52. /// <summary>
  53. /// The serialized reference to an external action.
  54. /// </summary>
  55. /// <value>The serialized reference field.</value>
  56. internal InputActionReference serializedReference => m_Reference;
  57. /// <summary>
  58. /// Initialize the property to contain the given action.
  59. /// </summary>
  60. /// <param name="action">An action.</param>
  61. /// <remarks>
  62. /// When the struct is serialized, it will serialize the given action as part of it.
  63. /// The <see cref="reference"/> property will return <c>null</c>.
  64. /// </remarks>
  65. public InputActionProperty(InputAction action)
  66. {
  67. m_UseReference = false;
  68. m_Action = action;
  69. m_Reference = null;
  70. }
  71. /// <summary>
  72. /// Initialize the property to use the given action reference.
  73. /// </summary>
  74. /// <param name="reference">Reference to an <see cref="InputAction"/>.</param>
  75. /// <remarks>
  76. /// When the struct is serialized, it will only serialize a reference to
  77. /// the given <paramref name="reference"/> object.
  78. /// </remarks>
  79. public InputActionProperty(InputActionReference reference)
  80. {
  81. m_UseReference = true;
  82. m_Action = null;
  83. m_Reference = reference;
  84. }
  85. /// <summary>
  86. /// Compare two action properties to see whether they refer to the same action.
  87. /// </summary>
  88. /// <param name="other">Another action property.</param>
  89. /// <returns>True if both properties refer to the same action.</returns>
  90. public bool Equals(InputActionProperty other)
  91. {
  92. return m_Reference == other.m_Reference &&
  93. m_UseReference == other.m_UseReference &&
  94. m_Action == other.m_Action;
  95. }
  96. /// <summary>
  97. /// Check whether the property refers to the same action.
  98. /// </summary>
  99. /// <param name="other">An action.</param>
  100. /// <returns>True if <see cref="action"/> is the same as <paramref name="other"/>.</returns>
  101. public bool Equals(InputAction other)
  102. {
  103. return ReferenceEquals(action, other);
  104. }
  105. /// <summary>
  106. /// Check whether the property references the same action.
  107. /// </summary>
  108. /// <param name="other">An action reference.</param>
  109. /// <returns>True if the property and <paramref name="other"/> reference the same action.</returns>
  110. public bool Equals(InputActionReference other)
  111. {
  112. return m_Reference == other;
  113. }
  114. /// <summary>
  115. /// Check whether the given object is an InputActionProperty referencing the same action.
  116. /// </summary>
  117. /// <param name="obj">An object or <c>null</c>.</param>
  118. /// <returns>True if the given <paramref name="obj"/> is an InputActionProperty equivalent to this one.</returns>
  119. /// <seealso cref="Equals(InputActionProperty)"/>
  120. public override bool Equals(object obj)
  121. {
  122. if (m_UseReference)
  123. return Equals(obj as InputActionReference);
  124. return Equals(obj as InputAction);
  125. }
  126. /// <summary>
  127. /// Compute a hash code for the object.
  128. /// </summary>
  129. /// <returns>A hash code.</returns>
  130. public override int GetHashCode()
  131. {
  132. if (m_UseReference)
  133. return m_Reference != null ? m_Reference.GetHashCode() : 0;
  134. return m_Action != null ? m_Action.GetHashCode() : 0;
  135. }
  136. /// <summary>
  137. /// Compare the two properties for equivalence.
  138. /// </summary>
  139. /// <param name="left">The first property.</param>
  140. /// <param name="right">The second property.</param>
  141. /// <returns>True if the two action properties are equivalent.</returns>
  142. /// <seealso cref="Equals(InputActionProperty)"/>
  143. public static bool operator==(InputActionProperty left, InputActionProperty right)
  144. {
  145. return left.Equals(right);
  146. }
  147. /// <summary>
  148. /// Compare the two properties for not being equivalent.
  149. /// </summary>
  150. /// <param name="left">The first property.</param>
  151. /// <param name="right">The second property.</param>
  152. /// <returns>True if the two action properties are not equivalent.</returns>
  153. /// <seealso cref="Equals(InputActionProperty)"/>
  154. public static bool operator!=(InputActionProperty left, InputActionProperty right)
  155. {
  156. return !left.Equals(right);
  157. }
  158. [SerializeField] private bool m_UseReference;
  159. [SerializeField] private InputAction m_Action;
  160. [SerializeField] private InputActionReference m_Reference;
  161. }
  162. }