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 Axis
If a processor requires to be run on every read, override caching
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
Remarks
The type can be determined with value
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 with Read
Methods
CompareValue(void*, void*)
Compared values in state buffers.
Declaration
public override bool CompareValue(void* firstStatePtr, void* secondStatePtr)
Parameters
Type | Name | Description |
---|---|---|
void* | firstStatePtr | The first state buffer to read value from. |
void* | secondStatePtr | The second state buffer to read value from. |
Returns
Type | Description |
---|---|
bool | True if the buffer values match. False if they differ. |
Overrides
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.
Examples
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");
}
}
ProcessValue(TValue)
Applies all control processors to the passed value.
Declaration
public TValue ProcessValue(TValue value)
Parameters
Type | Name | Description |
---|---|---|
TValue | value | value to run processors on. |
Returns
Type | Description |
---|---|
TValue | The processed value. |
Remarks
Applies all control processors to the passed value.
ProcessValue(ref TValue)
Applies all control processors to the passed value.
Declaration
public void ProcessValue(ref TValue value)
Parameters
Type | Name | Description |
---|---|---|
TValue | value | value to run processors on. |
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 (default
ReadUnprocessedValue()
Read value from control
Declaration
public TValue ReadUnprocessedValue()
Returns
Type | Description |
---|---|
TValue | The controls current value. |
Remarks
This is the locally cached value. Use Read
See Also
ReadUnprocessedValueFromState(void*)
Read value from provided statePtr
.
Declaration
public abstract TValue ReadUnprocessedValueFromState(void* statePtr)
Parameters
Type | Name | Description |
---|---|---|
void* | statePtr | State pointer to read from. |
Returns
Type | Description |
---|---|
TValue | The controls current value. |
Remarks
Read value from provided statePtr
without any caching.
See Also
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 unprocessed
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 Axis
If a processor requires to be run on every read, override caching
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)
Read the control's final, processed value from the given buffer and return the value as an object.
Declaration
public override object ReadValueFromBufferAsObject(void* buffer, int bufferSize)
Parameters
Type | Name | Description |
---|---|---|
void* | buffer | Buffer to read the value from. |
int | bufferSize | Size of |
Returns
Type | Description |
---|---|
object | The control's value as stored in |
Overrides
Remarks
Read the control's final, processed value from the given buffer and return the value as an object.
This method allocates GC memory and should not be used during normal gameplay operation.
Exceptions
Type | Condition |
---|---|
Argument |
|
Argument |
|
See Also
ReadValueFromPreviousFrame()
Get the control's value from the previous frame (previous
Declaration
public TValue ReadValueFromPreviousFrame()
Returns
Type | Description |
---|---|
TValue | The control's value in the previous frame. |
ReadValueFromState(void*)
Get the control's default value.
Declaration
public TValue ReadValueFromState(void* statePtr)
Parameters
Type | Name | Description |
---|---|---|
void* | statePtr | State containing the control's state |
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 (default
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 | State to read the value for the control from. |
Returns
Type | Description |
---|---|
object | The control's value as stored in |
Overrides
Remarks
Read the control's final, processed value from the given state and return the value as an object.
This method allocates GC memory and should not be used during normal gameplay operation.
Exceptions
Type | Condition |
---|---|
Argument |
|
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 |
---|---|
Argument |
|
Argument |
|
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, to store into the state. |
int | bufferSize | Size of |
void* | statePtr | State containing the control's state |
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 |
---|---|
Not |
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 to store in the state. |
void* | statePtr | State containing the control's state |
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 |
---|---|
Not |
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*)
Write a value into state at the given memory.
Declaration
public virtual void WriteValueIntoState(TValue value, void* statePtr)
Parameters
Type | Name | Description |
---|---|---|
TValue | value | Value for the control to store in the state. |
void* | statePtr | State containing the control's state |
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 |
---|---|
Not |
The control does not support writing. This is the case, for example, that compute values (such as the magnitude of a vector). |