Class InputValue
Wraps around values provided by input actions.
Namespace: UnityEngine.InputSystem
Assembly: Unity.InputSystem.dll
Syntax
public class InputValue
Remarks
This is a wrapper around InputAction.CallbackContext chiefly for use with GameObject messages (i.e. SendMessage(string, object)). It exists so that action callback data can be represented as an object, can be reused, and shields the receiver from having to know about action callback specifics.
Properties
isPressed
Check if the action button is pressed.
Declaration
public bool isPressed { get; }
Property Value
Type | Description |
---|---|
bool |
Remarks
True if the button is activated over the button threshold. False otherwise
The following example check if a button is pressed when receiving a PlayerInput message.
The given InputValue
is only valid for the duration of the callback. Storing the InputValue
references somewhere and calling Get<T>() later does not work correctly.
Examples
[RequireComponent(typeof(PlayerInput))]
public class MyPlayerLogic : MonoBehaviour
{
// 'Fire' input action has been triggered.
public void OnFire(InputValue value)
{
if (value.isPressed)
FireWeapon();
}
public void FireWeapon()
{
// Weapon firing code
}
}
See Also
Methods
Get()
Read the current value as an object.
Declaration
public object Get()
Returns
Type | Description |
---|---|
object | The current value in the form of a boxed object. |
Remarks
This method allocates GC memory and will thus create garbage. If used during gameplay, it will lead to GC spikes.
See Also
Get<TValue>()
Read the current value of the action.
Declaration
public TValue Get<TValue>() where TValue : struct
Returns
Type | Description |
---|---|
TValue | The current value from the action cast to the specified type. |
Type Parameters
Name | Description |
---|---|
TValue | Type of value to read. This must correspond to the
valueType of the action or, if it is a composite, by the
valueType.
The type depends on what type of controls the action is bound to.
Common types are |
Remarks
The following example shows how to read a value from a PlayerInput message.
The given InputValue
is only valid for the duration of the callback. Storing the InputValue
references somewhere and calling Get<T>() later does not work correctly.
Examples
using UnityEngine;
using UnityEngine.InputSystem;
[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<Vector2>();
}
public void Update()
{
// Update transform from m_Move
}
}
Exceptions
Type | Condition |
---|---|
InvalidOperationException | The given type |