using System; namespace UnityEngine.InputSystem { /// /// A serializable property type that can either reference an action externally defined /// in an or define a new action directly on the property. /// /// /// This struct is meant to be used for serialized fields in MonoBehaviour and /// ScriptableObject classes. It has a custom property drawer attached to it /// that allows to switch between using the property as a reference and using it /// to define an action in place. /// /// /// /// public class MyBehavior : MonoBehaviour /// { /// // This can be edited in the inspector to either reference an existing /// // action or to define an action directly on the component. /// public InputActionProperty myAction; /// } /// /// /// /// /// [Serializable] public struct InputActionProperty : IEquatable, IEquatable, IEquatable { /// /// The action held on to by the property. /// /// The action object contained in the property. /// /// This property will return null if the property using a and /// the referenced action cannot be found. Also, it will be null if the property /// has been manually initialized with a null using /// . /// public InputAction action => m_UseReference ? m_Reference.action : m_Action; /// /// If the property contains a reference to the action, this property returns /// the reference. Otherwise it returns null. /// /// Reference to external input action, if defined. public InputActionReference reference => m_UseReference ? m_Reference : null; /// /// Initialize the property to contain the given action. /// /// An action. /// /// When the struct is serialized, it will serialize the given action as part of it. /// The property will return null. /// public InputActionProperty(InputAction action) { m_UseReference = false; m_Action = action; m_Reference = null; } /// /// Initialize the property to use the given action reference. /// /// Reference to an . /// /// When the struct is serialized, it will only serialize a reference to /// the given object. /// public InputActionProperty(InputActionReference reference) { m_UseReference = true; m_Action = null; m_Reference = reference; } /// /// Compare two action properties to see whether they refer to the same action. /// /// Another action property. /// True if both properties refer to the same action. public bool Equals(InputActionProperty other) { return m_Reference == other.m_Reference && m_UseReference == other.m_UseReference && m_Action == other.m_Action; } /// /// Check whether the property refers to the same action. /// /// An action. /// True if is the same as . public bool Equals(InputAction other) { return ReferenceEquals(action, other); } /// /// Check whether the property references the same action. /// /// An action reference. /// True if the property and reference the same action. public bool Equals(InputActionReference other) { return m_Reference == other; } /// /// Check whether the given object is an InputActionProperty referencing the same action. /// /// An object or null. /// True if the given is an InputActionProperty equivalent to this one. /// public override bool Equals(object obj) { if (m_UseReference) return Equals(obj as InputActionReference); return Equals(obj as InputAction); } /// /// Compute a hash code for the object. /// /// A hash code. public override int GetHashCode() { if (m_UseReference) return m_Reference.GetHashCode(); return m_Action.GetHashCode(); } /// /// Compare the two properties for equivalence. /// /// The first property. /// The second property. /// True if the two action properties are equivalent. /// public static bool operator==(InputActionProperty left, InputActionProperty right) { return left.Equals(right); } /// /// Compare the two properties for not being equivalent. /// /// The first property. /// The second property. /// True if the two action properties are not equivalent. /// public static bool operator!=(InputActionProperty left, InputActionProperty right) { return !left.Equals(right); } [SerializeField] private bool m_UseReference; [SerializeField] private InputAction m_Action; [SerializeField] private InputActionReference m_Reference; } }