iPhone iPad iTouch 旋轉(zhuǎn)設(shè)備都支持屏幕4個方向的任意旋轉(zhuǎn),那么強大的Unity3D 游戲引擎當(dāng)然也支持啦,雖然很多游戲都為了避免麻煩強制的不讓屏幕旋轉(zhuǎn)但是做為學(xué)習(xí)我們還是知道一下為好,因為Unity3D在處理屏幕旋轉(zhuǎn)實在是非常方便。
下面MOMO將以一個例子向各位盆友們介紹Unity3D 屏幕的哪些事兒。
強制屏幕四個方向不旋轉(zhuǎn)的方法
[代碼]c#/cpp/oc代碼:
3 |
iPhoneKeyboard.autorotateToPortrait = false; |
4 |
iPhoneKeyboard.autorotateToPortraitUpsideDown = false; |
7 |
iPhoneKeyboard.autorotateToLandscapeLeft = false; |
8 |
iPhoneKeyboard.autorotateToLandscapeRight = false; |
自動旋轉(zhuǎn)屏幕的方法,此方式適用于Unity3.3及一下的版本。
Input.deviceOrientation 可以得到當(dāng)前IOS 設(shè)備屏幕的方向狀態(tài)。
Screen.orientation 設(shè)置屏幕的反轉(zhuǎn)情況
[代碼]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 設(shè)置中直接設(shè)置屏幕旋轉(zhuǎn)。

下面的游戲例子,通過左邊的按鈕直接切換屏幕旋轉(zhuǎn)狀態(tài),右邊的按鈕打開iPhone輸入狀態(tài)框。

[代碼]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")) { |
42 |
//***個參數(shù) 默認(rèn)顯示 test |
43 |
//第二個參數(shù) 設(shè)置輸入框類型,這里為默認(rèn),什么都可以輸入 |
44 |
keyboard = iPhoneKeyboard.Open("test",iPhoneKeyboardType.Default); |
51 |
//輸入完畢后 點擊done 輸入輸入內(nèi)容 |
52 |
Debug.Log( keyboard.text) ; |
iPhoneKeyboardType 鍵盤類型幾個比較重要的參數(shù),盆友們可是輸入試一試就知道效果啦。我就不截圖了~
iPhoneKeyboardType.NumbersAndPunctuation : 輸入標(biāo)點符號與數(shù)字iPhoneKeyboardType.URL:輸入網(wǎng)址iPhoneKeyboardType.PhonePad:輸入電話iPhoneKeyboardType.NumberPad:輸入數(shù)字iPhoneKeyboardType.EmailAddress:輸入Email
屏幕方向不僅可以感應(yīng)IOS設(shè)備平面4個方向,還可以感應(yīng)屏幕上下方向。
屏幕面朝上:LandscapeLeft.FaceUp
屏幕面朝下:LandscapeLeft.FaceDown