Query sensors in code
To determine whether a particular sensor is present, you can use its .current getter.
// Determine if a Gyroscope sensor device is present.
if (UnityEngine.InputSystem.Gyroscope.current != null)
Debug.Log("Gyroscope present");
If several sensors of the same type are present, .current usually stays with the first sensor that became current until you remove that sensor. Most sensor controls are noisy, so adding a sensor or receiving sensor input doesn't change .current.
Unlike other devices, sensors are disabled by default. To enable a sensor, call InputSystem.EnableDevice.
InputSystem.EnableDevice(UnityEngine.InputSystem.Gyroscope.current);
To disable a sensor, call InputSystem.DisableDevice.
InputSystem.DisableDevice(UnityEngine.InputSystem.Gyroscope.current);
To check whether a sensor is currently enabled, use InputDevice.enabled.
if (UnityEngine.InputSystem.Gyroscope.current.enabled)
Debug.Log("Gyroscope is enabled");
Read the sensors of a gamepad
A gamepad can have its own motion sensors. Each sensor is a separate device. Use Gamepad.gyroscope and Gamepad.accelerometer to get the sensors of one gamepad. Don't use Gyroscope.current or Accelerometer.current for this. Each returns a single sensor for the whole system, so it can't tell you which gamepad a sensor belongs to.
// Both properties are null if the gamepad has no such sensor.
var gyroscope = gamepad.gyroscope;
if (gyroscope != null)
InputSystem.EnableDevice(gyroscope);
var accelerometer = gamepad.accelerometer;
if (accelerometer != null)
InputSystem.EnableDevice(accelerometer);
Keep these points in mind:
- Each property returns
nullif the gamepad has no such sensor, for example on a platform that doesn't report gamepad sensors. - If a gamepad has more than one sensor of a type, the property returns the first one in
InputSystem.devicesorder. - Like other sensors, a gamepad's sensors are disabled by default. Call
InputSystem.EnableDevicebefore you read them. - The property looks the sensor up each time you read it. After a sensor or a gamepad is added or removed, read the property again. To get notified when a device is added or removed, subscribe to
InputSystem.onDeviceChange.
Sample sensor frequency
Sensors sample continuously at a set interval. You can set or query the sampling frequency for each sensor using the samplingFrequency property. The frequency is expressed in Hertz (number of samples per second).
// Get sampling frequency of gyro.
var frequency = UnityEngine.InputSystem.Gyroscope.current.samplingFrequency;
// Set sampling frequency of gyro to sample 16 times per second.
UnityEngine.InputSystem.Gyroscope.current.samplingFrequency = 16;
Measure a device's acceleration
Use the accelerometer to measure the acceleration of a device. This is useful to control content by moving a device around. It reports the acceleration measured on a device both due to moving the device around, and due to gravity pulling the device down. You can use GravitySensor and LinearAccelerationSensor to get separate values for these. Values are affected by the Compensate Orientation setting.
The following code traces all input events on the Accelerometer.current device.
private InputEventTrace trace;
void StartTrace()
{
InputSystem.EnableDevice(Accelerometer.current);
trace = new InputEventTrace(Accelerometer.current);
trace.Enable();
}
void Update()
{
foreach (var e in trace)
{
//...
}
trace.Clear();
}
To subscribe directly to each acceleration reading as it arrives, without buffering a trace, enable the device and filter InputSystem.onEvent for events from Accelerometer.current:
void OnEnable()
{
InputSystem.EnableDevice(Accelerometer.current);
InputSystem.onEvent += OnInputEvent;
}
void OnDisable()
{
InputSystem.onEvent -= OnInputEvent;
InputSystem.DisableDevice(Accelerometer.current);
}
void OnInputEvent(InputEventPtr eventPtr, InputDevice device)
{
if (device != Accelerometer.current)
return;
if (!eventPtr.IsA<StateEvent>() && !eventPtr.IsA<DeltaStateEvent>())
return;
// The out overload confirms the event actually carries a value for this control:
// a DeltaStateEvent might only cover part of the device's state.
if (!Accelerometer.current.acceleration.ReadValueFromEvent(eventPtr, out var acceleration))
return;
// Process acceleration, or increment your own counter here.
}
Determine the orientation of a device
Use the attitude sensor to determine the orientation of a device. This is useful to control content by rotating a device. Values are affected by the Compensate Orientation setting.
To detect only whether the device is upright in portrait or landscape, or lying flat, refer to Get the physical orientation of a device.
On Android devices, there are two types of attitude sensors: RotationVector and GameRotationVector.
Some Android devices have both types of sensor, while other devices may only have one or the other type available. These two types of attitude sensor behave slightly differently to each other. You can read about the differences between them here.
Because of this variety in what type of rotation sensors are available across devices, when you require input from a rotation sensor on Android devices, you should include code that checks for your preferred type of rotation sensor with a fallback to the alternative type of rotation sensor if it is not present. For example:
AttitudeSensor attitudeSensor = InputSystem.GetDevice<AndroidRotationVector>();
if (attitudeSensor == null)
{
attitudeSensor = InputSystem.GetDevice<AndroidGameRotationVector>();
if (attitudeSensor == null)
Debug.LogError("AttitudeSensor is not available");
}
if (attitudeSensor != null)
InputSystem.EnableDevice(attitudeSensor);