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 effective action held on to by the property.
///
/// The effective action object contained in the property.
///
/// This property will return null if the property is 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 != null ? m_Reference.action : null : m_Action;
///
/// If the property is set to use 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;
///
/// The serialized loose action created in code serialized with this property.
///
/// The serialized action field.
internal InputAction serializedAction => m_Action;
///
/// The serialized reference to an external action.
///
/// The serialized reference field.
internal InputActionReference serializedReference => m_Reference;
///
/// 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 != null ? m_Reference.GetHashCode() : 0;
return m_Action != null ? m_Action.GetHashCode() : 0;
}
///
/// 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;
}
}