Unity移动端自动翻转及横竖屏的设置与检测
Unity手动设置
手动设置程序方向File- - ->Build Settings- - ->PlayerSettings- - ->Resolution and Presentation- - ->Orientation。如下图所示。

所能设置的值有:
- Portrait:设备在纵向模式,设备直立并home按钮在底部。
- PortraitUpsideDown:设备在纵向模式,但颠倒一下,设备直立并home按钮在顶部。
- LandscapeLeft:设备在横向模式,设备直立并home按钮在右边。
- LandscapeRight:设备在横向模式,设备直立并home按钮在左边。
- Auto Rotation:设备可以自由旋转,在上面的模式中自由切换。
代码设置与检测
使用代码设置检测,有两种方式可用:Screen.orientation或Input.deviceOrientation。
Screen.orientation
这是Unity自带的一个静态属性。其实就是上面介绍的手动设置的代码方式,只是其枚举多了一个Unknown。是屏幕的显示方向。
当手动设置为一个固定的模式时,是没有必要实时检测的,除非处于Auto Rotation模式。
具体检测用法如下面的示例:
if (Screen.orientation == ScreenOrientation.Portrait)
transform.localEulerAngles = new Vector3(0, 180, 90);
else

本文详细介绍如何在Unity中通过代码设置和检测屏幕方向,包括手动设置方法及使用Screen.orientation和Input.deviceOrientation属性进行方向控制和检测的技术要点。
最低0.47元/天 解锁文章
3602





