http://blog.csdn.net/xys289187120/article/details/6993977
Unity3D 遊戲引擎之感應IOS裝置旋轉與iPhone鍵盤事件
雨松MOMO原創文章如轉載,請註明:轉載至我的獨立網域名稱部落格雨松MOMO程式研究院,原文地址:http://www.xuanyusong.com/archives/556
iPhone iPad iTouch 旋轉裝置都支援螢幕4個方向的任意旋轉,那麼強大的Unity3D 遊戲引擎當然也支援啦,雖然很多遊戲都為了避免麻煩強制的不讓旋轉螢幕,但是做為學習我們還是知道一下為好,因為Unity3D在處理旋轉螢幕實在是非常方便,下面MOMO將以一個例子向各位盆友們介紹Unity3D 螢幕的哪些事兒~~。
強制螢幕四個方向不旋轉的方法
[csharp] view plaincopy
- void Start () {
- //縱向 上下 兩個方向
- iPhoneKeyboard.autorotateToPortrait = false;
- iPhoneKeyboard.autorotateToPortraitUpsideDown = false;
-
- //橫向 上下兩個方向
- iPhoneKeyboard.autorotateToLandscapeLeft = false;
- iPhoneKeyboard.autorotateToLandscapeRight = false;
- }
自動旋轉螢幕的方法,此方式適用於Unity3.3及一下的版本。
Input.deviceOrientation 可以得到當前IOS 裝置螢幕的方向狀態。
Screen.orientation 設定螢幕的反轉情況
[csharp] view plaincopy
- void Update () {
- //處理橫向兩個方向旋轉
- if(Input.deviceOrientation == DeviceOrientation.LandscapeLeft)
- {
- if (Screen.orientation != ScreenOrientation.LandscapeLeft) {
- Screen.orientation = ScreenOrientation.LandscapeLeft;
- }
- }else if(Input.deviceOrientation == DeviceOrientation.LandscapeRight)
- {
- if (Screen.orientation != ScreenOrientation.LandscapeRight) {
- Screen.orientation = ScreenOrientation.LandscapeRight;
- }
-
- }else
- //處理縱向兩個方向的旋轉
- if(Input.deviceOrientation == DeviceOrientation.Portrait)
- {
- if (Screen.orientation != ScreenOrientation.Portrait) {
- Screen.orientation = ScreenOrientation.Portrait;
- }
- }else if(Input.deviceOrientation == DeviceOrientation.PortraitUpsideDown)
- {
- if (Screen.orientation != ScreenOrientation.PortraitUpsideDown) {
- Screen.orientation = ScreenOrientation.PortraitUpsideDown;
- }
- }
- }
3.4及以上的版本可以在Setting for IOS 設定中直接設定旋轉螢幕。
下面的遊戲例子,通過左邊的按鈕直接切換畫面旋轉狀態,右邊的按鈕開啟iPhone輸入狀態框。
[csharp] view plaincopy
- using UnityEngine;
- using System.Collections;
-
- public class Main : MonoBehaviour {
-
- //鍵盤輸入
- private iPhoneKeyboard keyboard;
-
- //字型皮膚
- public GUISkin fontSkin;
-
- // Use this for initialization
- void Start () {
- }
-
- // Update is called once per frame
- void Update () {
- }
-
-
- void OnGUI() {
- //設定皮膚
- GUI.skin = fontSkin;
-
- //強制螢幕縱向
- if (GUI.Button(new Rect(10, 10, 300, 100), "change LandscapeLeft")) {
- Screen.orientation = ScreenOrientation.LandscapeLeft;
- }else if (GUI.Button(new Rect(10, 110, 300, 100), "change LandscapeRight")) {
- Screen.orientation = ScreenOrientation.LandscapeRight;
- }else
-
- //強制螢幕橫向
- if (GUI.Button(new Rect(10, 210, 300, 100), "change Portrait")) {
- Screen.orientation = ScreenOrientation.Portrait;
- }else if (GUI.Button(new Rect(10, 310, 300, 100), "change PortraitUpsideDown")) {
- Screen.orientation = ScreenOrientation.PortraitUpsideDown;
- }
-
-
- if (GUI.Button(new Rect(320, 10, 300, 100), "open Keyboard")) {
- //開啟iphone輸入框
- //第一個參數 預設顯示 test
- //第二個參數 設定輸入框類型,這裡為預設,什麼都可以輸入
- keyboard = iPhoneKeyboard.Open("test",iPhoneKeyboardType.Default);
-
- }
-
- if(keyboard != null){
-
- if (keyboard.done){
- //輸入完畢後 點擊done 輸入輸入內容
- Debug.Log( keyboard.text) ;
- }
- }
-
- }
- }
iPhoneKeyboardType 鍵盤類型幾個比較重要的參數,盆友們可是輸入試一試就知道效果啦。我就不了~
iPhoneKeyboardType.NumbersAndPunctuation : 輸入標點符號與數字
iPhoneKeyboardType.URL:輸入網址
iPhoneKeyboardType.PhonePad:輸入電話
iPhoneKeyboardType.NumberPad:輸入數字
iPhoneKeyboardType.EmailAddress:輸入Email
螢幕方向不僅可以感應IOS裝置平面4個方向,還可以感應螢幕上下方向。 螢幕面朝上:LandscapeLeft.FaceUp
螢幕面朝下:LandscapeLeft.FaceDown