首页 >Unity3D频道 >【Unity3D研究院之游戏开发】 > Unity3D研究院之感应IOS设备旋转与iPhone键盘事件(十六)
2012
05-01

Unity3D研究院之感应IOS设备旋转与iPhone键盘事件(十六)

iPhone iPad iTouch 旋转设备都支持屏幕4个方向的任意旋转,那么强大的Unity3D 游戏引擎当然也支持啦,虽然很多游戏都为了避免麻烦强制的不让屏幕旋转,但是做为学习我们还是知道一下为好,因为Unity3D在处理屏幕旋转实在是非常方便,下面MOMO将以一个例子向各位盆友们介绍Unity3D 屏幕的哪些事儿~~。

强制屏幕四个方向不旋转的方法

	void Start () {
		//纵向 上下 两个方向
		iPhoneKeyboard.autorotateToPortrait = false;
		iPhoneKeyboard.autorotateToPortraitUpsideDown = false;

		//横向 上下两个方向
		iPhoneKeyboard.autorotateToLandscapeLeft = false;
		iPhoneKeyboard.autorotateToLandscapeRight = false;
	}

 

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

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

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

 

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 设置中直接设置屏幕旋转。

 

Unity3D研究院之感应IOS设备旋转与iPhone键盘事件(十六) - 雨松MOMO程序研究院 - 1

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

 

Unity3D研究院之感应IOS设备旋转与iPhone键盘事件(十六) - 雨松MOMO程序研究院 - 2

 

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

 

Unity3D研究院之感应IOS设备旋转与iPhone键盘事件(十六) - 雨松MOMO程序研究院 - 3

 


屏幕方向不仅可以感应IOS设备平面4个方向,还可以感应屏幕上下方向。

屏幕面朝上:LandscapeLeft.FaceUp
屏幕面朝下:LandscapeLeft.FaceDown

最后欢迎各位盆友可以和MOMO一起讨论Unity3D游戏开发,总的来说这一章还是比较简单的,代码我就不上传了。哇咔咔~强烈感谢四角线技术大牛~  我愿和 大家好好学习!!!

 

最后编辑:
作者:雨松MOMO
专注移动互联网,Unity3D游戏开发
捐 赠写博客不易,如果您想请我喝一杯星巴克的话?就进来看吧!

Unity3D研究院之感应IOS设备旋转与iPhone键盘事件(十六)》有 11 条评论

  1. helloWorld说:

    竖版游戏,中间有个场景时横版的,使用UI相机z轴旋转90度,屏幕转成功了之后,锚点什么的就乱掉了,这是因为什么原因啊

  2. 请问有安卓机上的应用吗

  3. Level_z说:

    请问如果向让ios的虚拟键盘弹出来的时候把整个程序往上移动以免遮住输入框要怎么做啊?

  4. 275727971@qq.com说:

    怎么没有工程文件啊

  5. un说:

    – -是四角钱吧。。
    成四角线了。。。

留下一个回复

你的email不会被公开。 必填项已用 * 标注。