纳金网

标题: 安卓 iphone 自动设置朝向 [打印本页]

作者: 狂风大尉    时间: 2014-8-31 01:06
标题: 安卓 iphone 自动设置朝向
此脚本主要为了 适应如下情况
设备无非两种情况
横宽竖窄、横窄竖宽
可能你在编译的时候设置了横屏 理想情况下是
横窄竖宽      实际拿到设备发现情况是 横宽竖窄
______                                                 _____________
|      ↓    |                                                |         ↓                |
|      ↓    |                                                |         ↓                |
|      ↓    |                                                _____________
______

所以有了此脚本的诞生 设置横屏 必然是宽大于高 竖屏 则高大于款
脚本如下
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using UnityEngine;


  6. public static class MethodRegister
  7. {
  8.     /// <summary>
  9.     /// 横屏
  10.     /// </summary>
  11.     public static const int LANDSCAPE = 0;
  12.     /// <summary>
  13.     /// 竖屏
  14.     /// </summary>
  15.     public static const int PORTRAIT = 1;
  16.     /// <summary>
  17.     /// 自适应朝向
  18.     ///
  19.     /// <param name="type">屏幕朝向类型 0横屏 1纵屏</param>
  20.     /// </summary>
  21.     public static void ReOrientation(this MonoBehaviour mono,int type)
  22.     {
  23.         int value1;
  24.         int value2;
  25.         switch (type) {
  26.             case LANDSCAPE:
  27.                 value1 = Screen.height;
  28.                 value2 = Screen.width;
  29.                 break;
  30.             case PORTRAIT:
  31.                 value1 = Screen.width;
  32.                 value2 = Screen.height;               
  33.                 break;
  34.             default:
  35.                 return;
  36.         }
  37.         #if UNITY_ANDROID
  38.         if (value1 > value2)
  39.         {
  40.             if (Screen.orientation == ScreenOrientation.Landscape || Screen.orientation == ScreenOrientation.LandscapeLeft || Screen.orientation == ScreenOrientation.LandscapeRight)
  41.             {
  42.                 Screen.orientation = ScreenOrientation.Portrait;
  43.             }
  44.             else
  45.             {
  46.                 Screen.orientation = ScreenOrientation.Landscape;
  47.             }
  48.         }
  49.         #endif
  50.         #if UNITY_IPHONE
  51.         if (value1 > value2) {
  52.             if (Screen.orientation == ScreenOrientation.Landscape || Screen.orientation == ScreenOrientation.LandscapeLeft || Screen.orientation == ScreenOrientation.LandscapeRight)
  53.             {
  54.                 Screen.orientation = ScreenOrientation.Portrait;
  55.             }
  56.             else {
  57.                 Screen.orientation = ScreenOrientation.Landscape;
  58.             }
  59.         }
  60.         #endif

  61.     }
  62. }

复制代码
在项目启动时 在Awake()方法中调用this.ReOrientation(0 or 1)即可
作者: hyui    时间: 2014-8-31 01:54
Thanks for sharing !
作者: kaifly8888    时间: 2014-9-19 22:05
强大的文章,谢谢




欢迎光临 纳金网 (http://wwww.narkii.com/club/) Powered by Discuz! X2.5