下面MOMO将以一个例子向各位盆友们介绍Unity3D 屏幕的哪些事儿。
强制屏幕四个方向不旋转的方法
[代码]c#/cpp/oc代码:
| 3 | iPhoneKeyboard.autorotateToPortrait = false; |
| 4 | iPhoneKeyboard.autorotateToPortraitUpsideDown = false; |
| 7 | iPhoneKeyboard.autorotateToLandscapeLeft = false; |
| 8 | iPhoneKeyboard.autorotateToLandscapeRight = false; |
自动旋转屏幕的方法,此方式适用于Unity3.3及一下的版本。
Input.deviceOrientation 可以得到当前IOS 设备屏幕的方向状态。
Screen.orientation 设置屏幕的反转情况
[代码]c#/cpp/oc代码:
| 03 | if(Input.deviceOrientation == DeviceOrientation.LandscapeLeft) |
| 05 | if (Screen.orientation != ScreenOrientation.LandscapeLeft) { |
| 06 | Screen.orientation = ScreenOrientation.LandscapeLeft; |
| 08 | }else if(Input.deviceOrientation == DeviceOrientation.LandscapeRight) |
| 10 | if (Screen.orientation != ScreenOrientation.LandscapeRight) { |
| 11 | Screen.orientation = ScreenOrientation.LandscapeRight; |
| 16 | if(Input.deviceOrientation == DeviceOrientation.Portrait) |
| 18 | if (Screen.orientation != ScreenOrientation.Portrait) { |
| 19 | Screen.orientation = ScreenOrientation.Portrait; |
| 21 | }else if(Input.deviceOrientation == DeviceOrientation.PortraitUpsideDown) |
| 23 | if (Screen.orientation != ScreenOrientation.PortraitUpsideDown) { |
| 24 | Screen.orientation = ScreenOrientation.PortraitUpsideDown; |
3.4及以上的版本可以在Setting for IOS 设置中直接设置屏幕旋转。
![]()
下面的游戏例子,通过左边的按钮直接切换屏幕旋转状态,右边的按钮打开iPhone输入状态框。
![]()
[代码]c#/cpp/oc代码:
| 02 | using System.Collections; |
| 04 | public class Main : MonoBehaviour { |
| 07 | private iPhoneKeyboard keyboard; |
| 10 | public GUISkin fontSkin; |
| 12 | // Use this for initialization |
| 16 | // Update is called once per frame |
| 26 | if (GUI.Button(new Rect(10, 10, 300, 100), "change LandscapeLeft")) { |
| 27 | Screen.orientation = ScreenOrientation.LandscapeLeft; |
| 28 | }else if (GUI.Button(new Rect(10, 110, 300, 100), "change LandscapeRight")) { |
| 29 | Screen.orientation = ScreenOrientation.LandscapeRight; |
| 33 | if (GUI.Button(new Rect(10, 210, 300, 100), "change Portrait")) { |
| 34 | Screen.orientation = ScreenOrientation.Portrait; |
| 35 | }else if (GUI.Button(new Rect(10, 310, 300, 100), "change PortraitUpsideDown")) { |
| 36 | Screen.orientation = ScreenOrientation.PortraitUpsideDown; |
| 40 | if (GUI.Button(new Rect(320, 10, 300, 100), "open Keyboard")) { |
| 43 | //第二个参数 设置输入框类型,这里为默认,什么都可以输入 |
| 44 | keyboard = iPhoneKeyboard.Open("test",iPhoneKeyboardType.Default); |
| 52 | Debug.Log( keyboard.text) ; |
iPhoneKeyboardType 键盘类型几个比较重要的参数,盆友们可是输入试一试就知道效果啦。我就不截图了~
iPhoneKeyboardType.NumbersAndPunctuation : 输入标点符号与数字iPhoneKeyboardType.URL:输入网址iPhoneKeyboardType.PhonePad:输入电话iPhoneKeyboardType.NumberPad:输入数字iPhoneKeyboardType.EmailAddress:输入Email
![]()
屏幕方向不仅可以感应IOS设备平面4个方向,还可以感应屏幕上下方向。
屏幕面朝上:LandscapeLeft.FaceUp
屏幕面朝下:LandscapeLeft.FaceDown