Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class InputValue
/// Read the value as an object.
/// </summary>
/// <remarks>
/// This method allocates GC memory and will thus created garbage. If used during gameplay,
/// This method allocates GC memory and will thus create garbage. If used during gameplay,
/// it will lead to GC spikes.
/// </remarks>
/// <returns>The current value in the form of a boxed object.</returns>
Expand All @@ -35,6 +35,43 @@ public object Get()
}

////TODO: add automatic conversions
/// <summary>
/// Read the value of the action.
/// </summary>
/// <returns>The current value from the action cast to the specified type.</returns>
/// <typeparam name="TValue">Type of value to read. This must correspond to the
/// expected by either <see cref="control"/> or, if it is a composite, by the
/// <see cref="InputBindingComposite"/> in use.
/// Common types are float and Vector2, and depend on the type of the associated action</typeparam>
/// <exception cref="InvalidOperationException">The given type <typeparamref name="TValue"/>
/// does not match the value type expected by the control or binding composite.</exception>
/// <remarks>
/// The following example shows how to read a value from a <see cref="PlayerInput"/> message.
///
/// <example>
/// <code>
/// [RequireComponent(typeof(PlayerInput))]
/// public class MyPlayerLogic : MonoBehaviour
/// {
/// private Vector2 m_Move;
///
/// // 'Move' input action has been triggered.
/// public void OnMove(InputValue value)
/// {
/// // Read value from control. The type depends on what type of controls the action is bound to.
/// m_Move = value.Get&lt;Vector2&gt;();
/// }
///
/// public void OnUpdate()
/// {
/// // Update transform from m_Move
/// }
/// }
/// </code>
/// </example>
/// The given InputValue is only valid for the duration of the callback. Storing the InputValue references somewhere and calling Get&lt;T&gt;() later does not work correctly.
/// </remarks>
/// <seealso cref="CallbackContext.ReadValue{TValue}"/>
public TValue Get<TValue>()
where TValue : struct
{
Expand All @@ -45,6 +82,36 @@ public TValue Get<TValue>()
}

////TODO: proper message if value type isn't right
/// <summary>
/// Check if the action button is pressed
/// </summary>
/// <returns>True if the button is activated over the button threshold. False otherwise</returns>
/// <remarks>
/// The following example shows how to read a value from a <see cref="PlayerInput"/> message.
///
/// <example>
/// <code>
/// [RequireComponent(typeof(PlayerInput))]
/// public class MyPlayerLogic : MonoBehaviour
/// {
/// private bool m_Fire;
///
/// // 'Fire' input action has been triggered.
/// public void OnFire(InputValue value)
/// {
/// m_Fire = value.isPressed;
/// }
///
/// public void OnUpdate()
/// {
/// // Perform fire action if m_Fire is true
/// }
/// }
/// </code>
/// </example>
/// The given InputValue is only valid for the duration of the callback. Storing the InputValue references somewhere and calling Get&lt;T&gt;() later does not work correctly.
/// </remarks>
/// <seealso cref="ButtonControl.pressPointOrDefault"/>
public bool isPressed => Get<float>() >= ButtonControl.s_GlobalDefaultButtonPressPoint;

internal InputAction.CallbackContext? m_Context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,8 @@ public PlayerNotifications notificationBehavior
/// <remarks>
/// This array is only used if <see cref="notificationBehavior"/> is set to
/// <see cref="UnityEngine.InputSystem.PlayerNotifications.InvokeUnityEvents"/>.
///
/// The list of actions will be dependent on the <see cref="InputActionAsset"/> specified in the <see cref="PlayerInputEditor"/> UI.
/// </remarks>
public ReadOnlyArray<ActionEvent> actionEvents
{
Expand Down Expand Up @@ -1978,19 +1980,54 @@ private void SwitchControlSchemeInternal(ref InputControlScheme controlScheme, p
}
}

/// <summary>
/// An event associated with an PlayerInput action.
/// </summary>
/// <remarks>
/// Represents an event invoked in response to actions being triggered.
///
/// Contains the Id and name of the action being triggered and the associated UnityAction to handle the action response.
/// </remarks>
/// <example>
/// <code>
/// </code>
/// </example>
/// <seealso cref="PlayerInput.actionEvents"/>
[Serializable]
public class ActionEvent : UnityEvent<InputAction.CallbackContext>
{
/// <summary>
/// Action GUID string for the action that triggered the event.
/// </summary>
public string actionId => m_ActionId;

/// <summary>
/// Name of the action that triggered the event.
/// </summary>
public string actionName => m_ActionName;

[SerializeField] private string m_ActionId;
[SerializeField] private string m_ActionName;

/// <summary>
/// Construct an empty action event.
/// </summary>
/// <remarks>
/// The event will not have an associated action.
/// </remarks>
public ActionEvent()
{
}

/// <summary>
/// Construct an action event and associated it with an action.
/// </summary>
/// <remarks>
/// The event will be associated with the specified action. The action must be part of an action asset.
/// </remarks>
/// <param name="action">The action to associate with the event. The action must be part of an action asset.</param>
/// <exception cref="ArgumentNullException">The action is null.</exception>
/// <exception cref="ArgumentException">The action is not part of an action asset.</exception>
public ActionEvent(InputAction action)
{
if (action == null)
Expand All @@ -2004,6 +2041,14 @@ public ActionEvent(InputAction action)
m_ActionName = $"{action.actionMap.name}/{action.name}";
}

/// <summary>
/// Construct an action event and associated it with an action by GUID.
/// </summary>
/// <remarks>
/// The event will be associated with the specified action.
/// </remarks>
/// <param name="actionGUID">Action GUID</param>
/// <param name="name">name of the action</param>
public ActionEvent(Guid actionGUID, string name = null)
{
m_ActionId = actionGUID.ToString();
Expand Down
Loading