Rasagar/Library/PackageCache/com.unity.inputsystem/Documentation~/Sensors.md
2024-08-26 23:07:20 +03:00

12 KiB

uid
input-system-sensors

Sensor support

Sensors are InputDevices that measure environmental characteristics of the device that the content is running on. Unity currently supports sensors on iOS and Android. Android supports a wider range of sensors than iOS.

Note: To test your app on iOS or Android in the editor with sensor input from your mobile device, you can use the Unity Remote as described here. This currently supports Accelerometer, Gyroscope, GravitySensor, AttitudeSensor, and LinearAccelerationSensor.

To determine whether a particular sensor is present, you can use its .current getter.

// Determine if a Gyroscope sensor device is present.
if (Gyroscope.current != null)
    Debug.Log("Gyroscope present");

Unlike other devices, sensors are disabled by default. To enable a sensor, call InputSystem.EnableDevice()).

InputSystem.EnableDevice(Gyroscope.current);

To disable a sensor, call InputSystem.DisableDevice().

InputSystem.DisableDevice(Gyroscope.current);

To check whether a sensor is currently enabled, use InputDevice.enabled.

if (Gyroscope.current.enabled)
    Debug.Log("Gyroscope is enabled");

Each sensor Device implements a single Control which represents the data read by the sensor. The following sensors are available:

Device Android iOS WebGL Control Type
Accelerometer Yes Yes Yes(1) acceleration Vector3Control
Gyroscope Yes Yes Yes(1) angularVelocity Vector3Control
GravitySensor Yes Yes Yes(1) gravity Vector3Control
AttitudeSensor Yes Yes Yes(1) attitude QuaternionControl
LinearAccelerationSensor Yes Yes Yes(1) acceleration Vector3Control
MagneticFieldSensor Yes No No magneticField Vector3Control
LightSensor Yes No No lightLevel AxisControl
PressureSensor Yes No No atmosphericPressure AxisControl
ProximitySensor Yes No No distance AxisControl
HumiditySensor Yes No No relativeHumidity AxisControl
AmbientTemperatureSensor Yes No No ambientTemperature AxisControl
StepCounter Yes Yes No stepCounter IntegerControl

Notes:

  1. Sensor support for WebGL on Android and iOS devices is available in Unity 2021.2

Sampling 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 = Gyroscope.current.samplingFrequency;

// Set sampling frequency of gyro to sample 16 times per second.
Gyroscope.current.samplingFrequency = 16;

Accelerometer

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.

Gyroscope

Use the gyroscope to measure the angular velocity of a device. This is useful to control content by rotating a device. Values are affected by the Compensate Orientation setting.

GravitySensor

Use the gravity sensor to determine the direction of the gravity vector relative to a device. This is useful to control content by device orientation. This is usually derived from a hardware Accelerometer, by subtracting the effect of linear acceleration (see LinearAccelerationSensor). Values are affected by the Compensate Orientation setting.

AttitudeSensor

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.

Note: 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);

LinearAccelerationSensor

Use the accelerometer to measure the acceleration of a device. This is useful to control content by moving a device around. Linear acceleration is the acceleration of a device unaffected by gravity. This is usually derived from a hardware Accelerometer, by subtracting the effect of gravity (see GravitySensor). Values are affected by the Compensate Orientation setting.

MagneticFieldSensor

This Input Device represents the magnetic field that affects the device which is running the content. Values are in micro-Tesla (μT) and measure the ambient magnetic field in the X, Y, and Z axis.

LightSensor

This Input Device represents the ambient light measured by the device which is running the content. Value is in SI lux units.

PressureSensor

This Input Device represents the atmospheric pressure measured by the device which is running the content. Value is in in hPa (millibar).

ProximitySensor

This Input Device measures how close the device which is running the content is to the user. Phones typically use the proximity sensor to determine if the user is holding the phone to their ear or not. Values represent distance measured in centimeters.

NOTE: The Samsung devices' proximity sensor is only enabled during calls and not when using speakerphone or Bluetooth earphones. This means the lock screen function won't work, allowing the user to use the display during the call. It is important to note that the proximity sensor only works during non-speakerphone or non-Bluetooth calls, as it is designed to prevent accidental touches during calls. However, the proximity sensor can work slightly differently on different Samsung phones.

HumiditySensor

This Input Device represents the ambient air humidity measured by the device which is running the content. Values represent the relative ambient air humidity in percent.

AmbientTemperatureSensor

This Input Device represents the ambient air temperature measured by the device which is running the content. Values represent temperature in Celsius degrees.

StepCounter

This Input Device represents the user's footstep count as measured by the device which is running the content.

NOTE: To access the pedometer on iOS/tvOS devices, you need to enable the Motion Usage setting in the Input Settings.