初入江湖
- 麒麟币
- 38 枚
- 任务币
- 0 枚
- 威望
- 0 点
- 贡献
- 0 点
- 主题
- 9贴
- 在线时间
- 3 小时
|
unity3D引擎教程
下面小编将以一个例子向各位盆友们介绍Unity3D 屏幕的哪些事儿。
强制屏幕四个方向不旋转的方法
[代码]c#/cpp/oc代码:
1void 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 设置屏幕的反转情况
[代码]c#/cpp/oc代码:
01void Update () {
02 //处理横向两个方向旋转
03 if(Input.deviceOrientation == DeviceOrientation.LandscapeLeft)
04 {
05 if (Screen.orientation != ScreenOrientation.LandscapeLeft) {
06 Screen.orientation = ScreenOrientation.LandscapeLeft;
07 }
08 }else if(Input.deviceOrientation == DeviceOrientation.LandscapeRight)
09 {
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输入状态框。
[代码]c#/cpp/oc代码:
01using UnityEngine;
02using System.Collections;
03
04public class Main : MonoBehaviour {
05
06 //键盘输入
07 private iPhoneKeyboard keyboard;
08
09 //字体皮肤
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;
|
|