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

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


Variables
Unknown The orientation of the device cannot be determined.
Portrait The device is in portrait mode, with the device held upright and the home button at the bottom.
PortraitUpsideDown The device is in portrait mode but upside down, with the device held upright and the home button at the top.
LandscapeLeft The device is in landscape mode, with the device held upright and the home button on the right side.
LandscapeRight The device is in landscape mode, with the device held upright and the home button on the left side.
FaceUp The device is held parallel to the ground with the screen facing upwards.
FaceDown The 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;
          }
    }
}

图示:
04_Unity3D的输入(Input)——移动设备方向_第1张图片 04_Unity3D的输入(Input)——移动设备方向_第2张图片 04_Unity3D的输入(Input)——移动设备方向_第3张图片 04_Unity3D的输入(Input)——移动设备方向_第4张图片
Portrait PortraitUpsideDown LandscapeLeft LandscapeRight
FaceUp: 手机面朝天空;
FaceDown: 手机面朝地面;
Unknown:当前方向非以上七种 或者 无法获取设备方向。

你可能感兴趣的:(Unity3D)