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
InputActionto the Button withButton.RegisterInputAction(), or by specifying theInputActionupon instantiation. This causes theButtonto subscribe to theperformedandcanceledevents 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
boolpropertyButton.Valuereturns the most recently set value of theButton. AButtoncan also be implicitly converted to aboolvalue. - How long a
Buttonhas 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 theButtondoesn't return them more than once. - The
Buttonremembers 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
Buttonexposes 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
InputActionto theAxiswithAxis.RegisterInputAction(), or by specifying theInputActionupon instantiation. This causes theAxisto subscribe to theperformedandcanceledevents of saidInputAction, and removes the need to use manual methods listed above. TheInputActionMUST 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
floatpropertyAxis.Valuereturns the most recently set value of the Axis. AnAxiscan also be implicitly converted to afloatvalue. - The
boolpropertiesAxis.Positive,Axis.Neutral, andAxis.Negativecan be called on to evaluate the currentAxis' value for conditional statements. - How long an
Axishas 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
Axisexposes 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
InputActionto the TwinAxes withTwinAxes.RegisterInputAction(), or by specifying theInputActionupon instantiation. This causes theTwinAxesto subscribe to theperformedandcanceledevents of saidInputAction, and removes the need to use manual methods listed above. TheInputActionMUST have "Vector2" as its expected Control Type. Directly setting the value of oneTwinAxes.Axisis not permitted.
A TwinAxes whose value is reliably updated can be read from. A TwinAxes offers various ways of doing so. These include:
- The
Vector2propertyTwinAxes.Valuereturns the most recently set value of theTwinAxes. ATwinAxescan also be implicitly converted to aVector2value. - The
TwinAxes.Angle()method returns the angle of theTwinAxescompared to anotherVector2value. - The
TwinAxes.AxisXandTwinAxes.AxisYproperties allow for eachAxisto be directly accessed as described above. - The
boolpropertyTwinAxes.Neutralcan be called on to evaluate the currentTwinAxesvalue for conditional statements. - The class exposes an event (
TwinAxes.OnValueChanged), which can be subscribed to.