04_Unity3D的输入(Input)——移动设备方向

来源:互联网 发布:德国淘宝店铺起名 编辑:程序博客网 时间:2024/06/05 21:17

Input也可以获取当前移动设备的方向,不过只能获取不能使用Input修改,因为Input.deviceOrientation  属性为只读的。Unity在DeviceOrientation枚举中定义了7种方向,如下所示


Variables
UnknownThe orientation of the device cannot be determined.PortraitThe device is in portrait mode, with the device held upright and the home button at the bottom.PortraitUpsideDownThe device is in portrait mode but upside down, with the device held upright and the home button at the top.LandscapeLeftThe device is in landscape mode, with the device held upright and the home button on the right side.LandscapeRightThe device is in landscape mode, with the device held upright and the home button on the left side.FaceUpThe device is held parallel to the ground with the screen facing upwards.FaceDownThe device is held parallel to the ground with the screen facing downwards.大家可以使用如下脚本测试一下:
public class mono5 : MonoBehaviour {     void OnGUI(){          switch(Input.deviceOrientation){               case DeviceOrientation.FaceDown:                    GUI.Label(new Rect(100,100,100,100),"FaceDown");                    break;               case DeviceOrientation.FaceUp:                    GUI.Label(new Rect(100,100,100,100),"FaceUp");                    break;               case DeviceOrientation.LandscapeLeft:                    GUI.Label(new Rect(100,100,100,100),"LandscapeLeft");                    break;               case DeviceOrientation.LandscapeRight:                    GUI.Label(new Rect(100,100,100,100),"LandscapeRight");                    break;               case DeviceOrientation.Portrait:                    GUI.Label(new Rect(100,100,100,100),"Portrait");                    break;               case DeviceOrientation.PortraitUpsideDown:                    GUI.Label(new Rect(100,100,100,100),"PortraitUpsideDown");                    break;               case DeviceOrientation.Unknown:                    GUI.Label(new Rect(100,100,100,100),"Unknown");                    break;          }    }}

图示:
PortraitPortraitUpsideDownLandscapeLeftLandscapeRight
FaceUp:手机面朝天空;
FaceDown:手机面朝地面;
Unknown:当前方向非以上七种或者无法获取设备方向。

0 0