Get the physical orientation of a device
Detect whether the device is upright in portrait or landscape, or lying flat, from the device orientation sensor. Use it to change your layout when the user turns the device, or to react when the user puts the device down.
Use the DeviceOrientationSensor device to find out how the user holds the device that your application runs on. The sensor reports the orientation as a single DeviceOrientation value, such as Portrait or FaceUp.
The device orientation sensor replaces the legacy Input.deviceOrientation property. To measure how far the device is rotated, use the attitude sensor instead. For more information, refer to Determine the orientation of a device.
Read the orientation
Like other sensors, the device orientation sensor is disabled by default.
To read the orientation:
- Enable the sensor with
InputSystem.EnableDevice. - Read the value of the
orientationcontrol. - When you no longer need orientation updates, disable the sensor with
InputSystem.DisableDevice.
Example: Log when the orientation changes
The following example logs a message each time the device orientation changes:
using UnityEngine;
using UnityEngine.InputSystem;
/// <summary>
/// Example script that logs a message when the device orientation changes.
/// </summary>
public class DeviceOrientationChangeExample : MonoBehaviour
{
UnityEngine.InputSystem.DeviceOrientation m_LastOrientation;
void OnEnable()
{
if (DeviceOrientationSensor.current != null)
InputSystem.EnableDevice(DeviceOrientationSensor.current);
}
void OnDisable()
{
if (DeviceOrientationSensor.current != null)
InputSystem.DisableDevice(DeviceOrientationSensor.current);
}
void Update()
{
var sensor = DeviceOrientationSensor.current;
if (sensor == null || !sensor.enabled)
return;
var orientation = sensor.orientation.ReadValue();
if (orientation == m_LastOrientation)
return;
m_LastOrientation = orientation;
Debug.Log($"Device orientation changed to {orientation}.");
}
}
Avoid type name conflicts
Both the UnityEngine and UnityEngine.InputSystem namespaces define a DeviceOrientation type. If your script uses both namespaces, write UnityEngine.InputSystem.DeviceOrientation in full, as in the example, or add a using alias at the top of the script:
using DeviceOrientation = UnityEngine.InputSystem.DeviceOrientation;
Orientation values
The sensor reports one of the following values:
| Value | Description |
|---|---|
Unknown |
The orientation can't be determined. |
Portrait |
The device is upright, with the home button at the bottom. |
PortraitUpsideDown |
The device is upright and upside down, with the home button at the top. |
LandscapeLeft |
The device is upright in landscape, with the home button on the right. |
LandscapeRight |
The device is upright in landscape, with the home button on the left. |
FaceUp |
The device lies flat, with the screen facing up. |
FaceDown |
The device lies flat, with the screen facing down. |
Screen orientation
The sensor describes the physical orientation of the device, not the orientation of the screen. The Compensate Orientation setting doesn't change it. To get the orientation of the screen, use Screen.orientation.