iPhone iPad iTouch 旋轉設備都支持屏幕4個方向的任意旋轉,那么強大的Unity3D 游戲引擎當然也支持啦,雖然很多游戲都為了避免麻煩強制的不讓屏幕旋轉但是做為學習我們還是知道一下為好,因為Unity3D在處理屏幕旋轉實在是非常方便。
下面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