Unity3D 游戏引擎之感应IOS设备旋转与iPhone键盘事件(十六)【转】

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 plain copy
  1. void Start () {  
  2.     //纵向 上下 两个方向  
  3.     iPhoneKeyboard.autorotateToPortrait = false;  
  4.     iPhoneKeyboard.autorotateToPortraitUpsideDown = false;  
  5.       
  6.     //横向 上下两个方向  
  7.     iPhoneKeyboard.autorotateToLandscapeLeft = false;  
  8.     iPhoneKeyboard.autorotateToLandscapeRight = false;  
  9. }  


 

 

自动旋转屏幕的方法,此方式适用于Unity3.3及一下的版本。

Input.deviceOrientation 可以得到当前IOS 设备屏幕的方向状态。

Screen.orientation 设置屏幕的反转情况

 
[csharp]  view plain copy
  1. void Update () {  
  2.         //处理横向两个方向旋转  
  3.         if(Input.deviceOrientation == DeviceOrientation.LandscapeLeft)  
  4.         {  
  5.             if (Screen.orientation != ScreenOrientation.LandscapeLeft) {  
  6.                 Screen.orientation = ScreenOrientation.LandscapeLeft;  
  7.             }  
  8.         }else if(Input.deviceOrientation == DeviceOrientation.LandscapeRight)  
  9.         {  
  10.             if (Screen.orientation != ScreenOrientation.LandscapeRight) {  
  11.                 Screen.orientation = ScreenOrientation.LandscapeRight;  
  12.             }  
  13.               
  14.         }else   
  15.         //处理纵向两个方向的旋转  
  16.         if(Input.deviceOrientation == DeviceOrientation.Portrait)  
  17.         {  
  18.             if (Screen.orientation != ScreenOrientation.Portrait) {  
  19.                 Screen.orientation = ScreenOrientation.Portrait;  
  20.             }  
  21.         }else if(Input.deviceOrientation == DeviceOrientation.PortraitUpsideDown)  
  22.         {  
  23.             if (Screen.orientation != ScreenOrientation.PortraitUpsideDown) {  
  24.                 Screen.orientation = ScreenOrientation.PortraitUpsideDown;  
  25.             }  
  26.         }  
  27.     }  


 

 

 

3.4及以上的版本可以在Setting for IOS 设置中直接设置屏幕旋转。

 

 


 

 

下面的游戏例子,通过左边的按钮直接切换屏幕旋转状态,右边的按钮打开iPhone输入状态框。

 

 

 
 
 
[csharp]  view plain copy
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class Main : MonoBehaviour {  
  5.   
  6.     //键盘输入  
  7.     private iPhoneKeyboard keyboard;  
  8.       
  9.     //字体皮肤  
  10.     public GUISkin fontSkin;    
  11.       
  12.     // Use this for initialization  
  13.     void Start () {  
  14.     }  
  15.       
  16.     // Update is called once per frame  
  17.     void Update () {  
  18.     }  
  19.       
  20.       
  21.     void OnGUI() {  
  22.         //设置皮肤  
  23.         GUI.skin = fontSkin;    
  24.            
  25.         //强制屏幕纵向  
  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;  
  30.         }else   
  31.           
  32.         //强制屏幕横向  
  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;  
  37.         }     
  38.           
  39.           
  40.         if (GUI.Button(new Rect(320, 10, 300, 100), "open Keyboard"))  {   
  41.               //打开iphone输入框  
  42.               //第一个参数 默认显示 test  
  43.               //第二个参数 设置输入框类型,这里为默认,什么都可以输入  
  44.               keyboard = iPhoneKeyboard.Open("test",iPhoneKeyboardType.Default);  
  45.                 
  46.         }  
  47.           
  48.         if(keyboard != null){  
  49.               
  50.             if (keyboard.done){  
  51.                 //输入完毕后 点击done 输入输入内容  
  52.                 Debug.Log( keyboard.text)   ;  
  53.             }     
  54.         }  
  55.           
  56.     }  
  57. }  
 

 

iPhoneKeyboardType 键盘类型几个比较重要的参数,盆友们可是输入试一试就知道效果啦。我就不截图了~

iPhoneKeyboardType.NumbersAndPunctuation : 输入标点符号与数字

iPhoneKeyboardType.URL:输入网址

iPhoneKeyboardType.PhonePad:输入电话

iPhoneKeyboardType.NumberPad:输入数字

iPhoneKeyboardType.EmailAddress:输入Email


 


 
 
 
 
 
屏幕方向不仅可以感应IOS设备平面4个方向,还可以感应屏幕上下方向。
 
屏幕面朝上:LandscapeLeft.FaceUp
屏幕面朝下:LandscapeLeft.FaceDown

转载于:https://www.cnblogs.com/songtzu/archive/2013/04/09/3009535.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值