Class InputControl<TValue>
Base class for input controls with a specific value type.
Inherited Members
Namespace: UnityEngine.InputSystem
Assembly: Unity.InputSystem.dll
Syntax
public abstract class InputControl<TValue> : InputControl where TValue : struct
Type Parameters
Name | Description |
---|---|
TValue | Type of value captured by the control. Note that this does not mean that the control has to store data in the given value format. A control that captures float values, for example, may be stored in state as byte values instead. |
Properties
value
Returns the current value of the control after processors have been applied.
Declaration
public ref readonly TValue value { get; }
Property Value
Type | Description |
---|---|
TValue | The controls current value. |
Remarks
This can only be called on devices that have been added to the system (added).
If internal feature "USE_READ_VALUE_CACHING" is enabled, then this property implements caching to avoid applying processors when the underlying control has not changed. With this in mind, be aware of processors that use global state, such as the AxisDeadzoneProcessor. Unless the control unprocessed value has been changed, input system settings changed or ApplyParameterChanges() invoked, the processors will not run and calls to value will return the same result as previous calls.
If a processor requires to be run on every read, override cachingPolicy property in the processor and set it to EvaluateOnEveryRead.
To improve debugging try setting "PARANOID_READ_VALUE_CACHING_CHECKS" internal feature flag to check if cache value is still consistent.
Also note that this property returns the result as ref readonly. If custom control states are in use, i.e. any controls not shipped with the Input System package, be careful of accidental defensive copies https://docs.microsoft.com/en-us/dotnet/csharp/write-safe-efficient-code#avoid-defensive-copies.
See Also
valueSizeInBytes
Size in bytes of values that the control returns.
Declaration
public override int valueSizeInBytes { get; }
Property Value
Type | Description |
---|---|
int |
Overrides
See Also
valueType
Returns the underlying value type of this control.
Declaration
public override Type valueType { get; }
Property Value
Type | Description |
---|---|
Type | Type of values produced by the control. |
Overrides
Remarks
This is the type of values that are returned when reading the current value of a control or when reading a value of a control from an event.
See Also
Methods
CompareValue(void*, void*)
Compare the value of the control as read from firstStatePtr
to that read from
secondStatePtr
and return true if they are equal.
Declaration
public override bool CompareValue(void* firstStatePtr, void* secondStatePtr)
Parameters
Type | Name | Description |
---|---|---|
void* | firstStatePtr | Memory containing the control's stateBlock. |
void* | secondStatePtr | Memory containing the control's stateBlock |
Returns
Type | Description |
---|---|
bool | True if the value of the control is equal in both |
Overrides
Remarks
Unlike CompareValue(void*, void*), this method will have to do more than just compare the memory for the control in the two state buffers. It will have to read out state for the control and run the full processing machinery for the control to turn the state into a final, processed value. CompareValue is thus more costly than CompareValue(void*, void*).
This method will apply epsilons (Epsilon) when comparing floats.
See Also
FinishSetup()
Perform final initialization tasks after the control hierarchy has been put into place.
Declaration
protected override void FinishSetup()
Overrides
Remarks
This method can be overridden to perform control- or device-specific setup work. The most common use case is for looking up child controls and storing them in local getters.
public class MyDevice : InputDevice
{
public ButtonControl button { get; private set; }
public AxisControl axis { get; private set; }
protected override void OnFinishSetup()
{
// Cache controls in getters.
button = GetChildControl("button");
axis = GetChildControl("axis");
}
}</code></pre></example>
ProcessValue(TValue)
Declaration
public TValue ProcessValue(TValue value)
Parameters
Type | Name | Description |
---|---|---|
TValue | value |
Returns
Type | Description |
---|---|
TValue |
ProcessValue(ref TValue)
Applies all control processors to the passed value.
Declaration
public void ProcessValue(ref TValue value)
Parameters
Type | Name | Description |
---|---|---|
TValue | value |
Remarks
Use this overload when your state struct is large to avoid creating copies of the state.
ReadDefaultValue()
Get the control's default value.
Declaration
public TValue ReadDefaultValue()
Returns
Type | Description |
---|---|
TValue | The control's default value. |
Remarks
This is not necessarily equivalent to default(TValue)
. A control's default value is determined
by reading its value from the default state (defaultStatePtr) which in turn
is determined from settings in the control's registered layout (defaultState).
ReadUnprocessedValue()
Declaration
public TValue ReadUnprocessedValue()
Returns
Type | Description |
---|---|
TValue |
ReadUnprocessedValueFromState(void*)
Declaration
public abstract TValue ReadUnprocessedValueFromState(void* statePtr)
Parameters
Type | Name | Description |
---|---|---|
void* | statePtr |
Returns
Type | Description |
---|---|
TValue |
ReadUnprocessedValueFromStateWithCaching(void*)
Read value from provided statePtr
. Try cache result if possible.
Declaration
public TValue ReadUnprocessedValueFromStateWithCaching(void* statePtr)
Parameters
Type | Name | Description |
---|---|---|
void* | statePtr | State pointer to read from. |
Returns
Type | Description |
---|---|
TValue | The controls current value. |
Remarks
If statePtr
is "currentStatePtr", then read will be done via unprocessedValue property to improve performance.
See Also
ReadValue()
Returns the current value of the control after processors have been applied.
Declaration
public TValue ReadValue()
Returns
Type | Description |
---|---|
TValue | The controls current value. |
Remarks
This can only be called on devices that have been added to the system (added).
If internal feature "USE_READ_VALUE_CACHING" is enabled, then this property implements caching to avoid applying processors when the underlying control has not changed. With this in mind, be aware of processors that use global state, such as the AxisDeadzoneProcessor. Unless the control unprocessed value has been changed, input system settings changed or ApplyParameterChanges() invoked, the processors will not run and calls to value will return the same result as previous calls.
If a processor requires to be run on every read, override cachingPolicy property in the processor and set it to EvaluateOnEveryRead.
To improve debugging try setting "PARANOID_READ_VALUE_CACHING_CHECKS" internal feature flag to check if cache value is still consistent. https://docs.microsoft.com/en-us/dotnet/csharp/write-safe-efficient-code#avoid-defensive-copies.
See Also
ReadValueFromBufferAsObject(void*, int)
Declaration
public override object ReadValueFromBufferAsObject(void* buffer, int bufferSize)
Parameters
Type | Name | Description |
---|---|---|
void* | buffer | |
int | bufferSize |
Returns
Type | Description |
---|---|
object |
Overrides
ReadValueFromPreviousFrame()
Get the control's value from the previous frame (previousFrameStatePtr).
Declaration
public TValue ReadValueFromPreviousFrame()
Returns
Type | Description |
---|---|
TValue | The control's value in the previous frame. |
ReadValueFromState(void*)
Declaration
public TValue ReadValueFromState(void* statePtr)
Parameters
Type | Name | Description |
---|---|---|
void* | statePtr |
Returns
Type | Description |
---|---|
TValue |
ReadValueFromStateAsObject(void*)
Read the control's final, processed value from the given state and return the value as an object.
Declaration
public override object ReadValueFromStateAsObject(void* statePtr)
Parameters
Type | Name | Description |
---|---|---|
void* | statePtr |
Returns
Type | Description |
---|---|
object | The control's value as stored in |
Overrides
Remarks
This method allocates GC memory and should not be used during normal gameplay operation.
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
See Also
ReadValueFromStateIntoBuffer(void*, void*, int)
Read the control's final, processed value from the given state and store it in the given buffer.
Declaration
public override void ReadValueFromStateIntoBuffer(void* statePtr, void* bufferPtr, int bufferSize)
Parameters
Type | Name | Description |
---|---|---|
void* | statePtr | State to read the value for the control from. |
void* | bufferPtr | Buffer to store the value in. |
int | bufferSize | Size of |
Overrides
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentException |
|
See Also
ReadValueFromStateWithCaching(void*)
Read value from provided statePtr
and apply processors. Try cache result if possible.
Declaration
public TValue ReadValueFromStateWithCaching(void* statePtr)
Parameters
Type | Name | Description |
---|---|---|
void* | statePtr | State pointer to read from. |
Returns
Type | Description |
---|---|
TValue | The controls current value. |
Remarks
If statePtr
is "currentStatePtr", then read will be done via value property to improve performance.
See Also
WriteValueFromBufferIntoState(void*, int, void*)
Read a value from the given memory and store it as state.
Declaration
public override void WriteValueFromBufferIntoState(void* bufferPtr, int bufferSize, void* statePtr)
Parameters
Type | Name | Description |
---|---|---|
void* | bufferPtr | Memory containing value. |
int | bufferSize | Size of |
void* | statePtr | State containing the control's stateBlock. Will receive the state as converted from the given value. |
Overrides
Remarks
Writing values will NOT apply processors to the given value. This can mean that when reading a value from a control after it has been written to its state, the resulting value differs from what was written.
Exceptions
Type | Condition |
---|---|
NotSupportedException | The control does not support writing. This is the case, for example, that compute values (such as the magnitude of a vector). |
See Also
WriteValueFromObjectIntoState(object, void*)
Read a value object and store it as state in the given memory.
Declaration
public override void WriteValueFromObjectIntoState(object value, void* statePtr)
Parameters
Type | Name | Description |
---|---|---|
object | value | Value for the control. |
void* | statePtr | State containing the control's stateBlock. Will receive the state state as converted from the given value. |
Overrides
Remarks
Writing values will NOT apply processors to the given value. This can mean that when reading a value from a control after it has been written to its state, the resulting value differs from what was written.
Exceptions
Type | Condition |
---|---|
NotSupportedException | The control does not support writing. This is the case, for example, that compute values (such as the magnitude of a vector). |
See Also
WriteValueIntoState(TValue, void*)
Declaration
public virtual void WriteValueIntoState(TValue value, void* statePtr)
Parameters
Type | Name | Description |
---|---|---|
TValue | value | |
void* | statePtr |