- 最后登录
- 2019-12-2
- 注册时间
- 2012-8-25
- 阅读权限
- 90
- 积分
- 34660
- 纳金币
- 38268
- 精华
- 111
|
此脚本主要为了 适应如下情况
设备无非两种情况
横宽竖窄、横窄竖宽
可能你在编译的时候设置了横屏 理想情况下是
横窄竖宽 实际拿到设备发现情况是 横宽竖窄
______ _____________
| ↓ | | ↓ |
| ↓ | | ↓ |
| ↓ | _____________
______
所以有了此脚本的诞生 设置横屏 必然是宽大于高 竖屏 则高大于款
脚本如下- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using UnityEngine;
- public static class MethodRegister
- {
- /// <summary>
- /// 横屏
- /// </summary>
- public static const int LANDSCAPE = 0;
- /// <summary>
- /// 竖屏
- /// </summary>
- public static const int PORTRAIT = 1;
- /// <summary>
- /// 自适应朝向
- ///
- /// <param name="type">屏幕朝向类型 0横屏 1纵屏</param>
- /// </summary>
- public static void ReOrientation(this MonoBehaviour mono,int type)
- {
- int value1;
- int value2;
- switch (type) {
- case LANDSCAPE:
- value1 = Screen.height;
- value2 = Screen.width;
- break;
- case PORTRAIT:
- value1 = Screen.width;
- value2 = Screen.height;
- break;
- default:
- return;
- }
- #if UNITY_ANDROID
- if (value1 > value2)
- {
- if (Screen.orientation == ScreenOrientation.Landscape || Screen.orientation == ScreenOrientation.LandscapeLeft || Screen.orientation == ScreenOrientation.LandscapeRight)
- {
- Screen.orientation = ScreenOrientation.Portrait;
- }
- else
- {
- Screen.orientation = ScreenOrientation.Landscape;
- }
- }
- #endif
- #if UNITY_IPHONE
- if (value1 > value2) {
- if (Screen.orientation == ScreenOrientation.Landscape || Screen.orientation == ScreenOrientation.LandscapeLeft || Screen.orientation == ScreenOrientation.LandscapeRight)
- {
- Screen.orientation = ScreenOrientation.Portrait;
- }
- else {
- Screen.orientation = ScreenOrientation.Landscape;
- }
- }
- #endif
- }
- }
复制代码 在项目启动时 在Awake()方法中调用this.ReOrientation(0 or 1)即可 |
|