This Unity package contains a series of classes that help with the storage and processing of (user) inputs.
While Unity's own Input System is very useful for abstracting input on the hardware level, there are some limitations on the processing front. InputActions fire performed
and canceled
events when input is detected, but the context of these events is meant to be discarded as soon as they are processed. The classes in this package act as intermediary data structures to evaluate inputs over a longer range of time.
The Button
class stores pressed/released (bool
) input values, and offers some specialised helper methods to interpret button presses/releases over time.
A Button
's value can be updated in two primary ways:
- Manually, through the
Button.Press()
,Button.Release()
, orButton.Toggle()
methods. - By attaching an
InputAction
to the Button withButton.RegisterInputAction()
, or by specifying theInputAction
upon instantiation. This causes theButton
to subscribe to theperformed
andcanceled
events of saidInputAction
, and removes the need to use manual methods listed above.
A Button
whose value is reliably updated can be read from. A Button
offers various ways of doing so. These include:
- The
bool
propertyButton.Value
returns the most recently set value of theButton
. AButton
can also be implicitly converted to abool
value. - How long a
Button
has been pressed or released in frames (int
) or seconds (float
) is returned by methods such asButton.GetPressDurationFrames()
,Button.GetPressDurationSeconds()
,Button.GetReleaseDurationFrames()
, andButton.GetReleaseDurationSeconds()
. - Methods such as
Button.Pressed()
,Button.PressedOnCurrentFrame()
,Button.Released()
, andButton.ReleasedOnCurrentFrame()
help with detecting individual button presses/releases.Button.Accept()
or optional parameters in the prior methods can be used to acknowledge such button presses/releases, so theButton
doesn't return them more than once. - The
Button
remembers when it was last pressed/released. This can be used to acknowledge presses/releases that happened before the current frame, thus enabling "buffered" button presses. The methodsButton.PressedInFrameBuffer()
,Button.PressedInTimeBuffer()
,Button.ReleasedInFrameBuffer()
, andButton.ReleasedInTimeBuffer()
can be used for this. - The
Button
exposes events (Button.OnValueChanged
,Button.OnPressed
, andButton.OnReleased
), which can be subscribed to.
The Axis
class stores axial (float
) values, as used by triggers and sticks. (Using TwinAxes is recommended for sticks.) Like the Button
class, it offers helper methods to interpret
An Axis
' value can be updated in two primary ways:
- Manually, through the
Axis.SetValue()
method. - By attaching an
InputAction
to theAxis
withAxis.RegisterInputAction()
, or by specifying theInputAction
upon instantiation. This causes theAxis
to subscribe to theperformed
andcanceled
events of saidInputAction
, and removes the need to use manual methods listed above. TheInputAction
MUST have "Axis" as its expected Control Type.
An Axis
whose value is reliably updated can be read from. An Axis
offers various ways of doing so. These include:
- The
float
propertyAxis.Value
returns the most recently set value of the Axis. AnAxis
can also be implicitly converted to afloat
value. - The
bool
propertiesAxis.Positive
,Axis.Neutral
, andAxis.Negative
can be called on to evaluate the currentAxis
' value for conditional statements. - How long an
Axis
has been positive (> 0f), neutral (== 0f), or negative (< 0f) can be returned in frames (int
) or seconds (float
) using methods such asAxis.GetPositiveDurationFrames()
,Axis.GetPositiveDurationSeconds()
,Axis.GetNeutralDurationFrames()
,Axis.GetNeutralDurationSeconds()
,Axis.GetNegativeDurationFrames()
, andAxis.GetNegativeDurationSeconds()
. - The
Axis
exposes events (Axis.OnValueChanged
,Axis.OnPositive
,Axis.OnNeutral
, andAxis.OnNegative
), which can be subscribed to.
The TwinAxes
class stores a pair (Vector2
) of TwinAxes.Axis
instances, making it highly recommended for joy-/control-sticks. It has some dedicated methods to easily manage and read both TwinAxes.Axis
at once.
A TwinAxes
' value can be updated in two primary ways:
- Manually, through the
TwinAxes.SetValue()
method. - By attaching an
InputAction
to the TwinAxes withTwinAxes.RegisterInputAction()
, or by specifying theInputAction
upon instantiation. This causes theTwinAxes
to subscribe to theperformed
andcanceled
events of saidInputAction
, and removes the need to use manual methods listed above. TheInputAction
MUST have "Vector2" as its expected Control Type. Directly setting the value of oneTwinAxes.Axis
is not permitted.
A TwinAxes
whose value is reliably updated can be read from. A TwinAxes
offers various ways of doing so. These include:
- The
Vector2
propertyTwinAxes.Value
returns the most recently set value of theTwinAxes
. ATwinAxes
can also be implicitly converted to aVector2
value. - The
TwinAxes.Angle()
method returns the angle of theTwinAxes
compared to anotherVector2
value. - The
TwinAxes.AxisX
andTwinAxes.AxisY
properties allow for eachAxis
to be directly accessed as described above. - The
bool
propertyTwinAxes.Neutral
can be called on to evaluate the currentTwinAxes
value for conditional statements. - The class exposes an event (
TwinAxes.OnValueChanged
), which can be subscribed to.