Unity 窗口适配要怎么做呢?现在的设备比较多,尤其是安卓,光手机可能就有好几种不同的分辨率,更不要说平板了,虽然苹果分辨率就那么几个,但是从出了6以后,苹果的分辨率也比较多了,下面我就分享一下Unity屏幕适配方案供大家参考学习。

unity中屏幕适配一直都没有一个完美的解决方案,在这里写了一个编辑器模式运行的类,可以在编辑器模式下方便的调整好UI位置然后在运行时候自动对齐。
具体代码实现如下AutoLayout.cs:

  1. using UnityEngine;
  2. using System.Collections;
  3.   
  4. public enum QuadrantLayout{
  5.     Quadrant1,
  6.     Quadrant2,
  7.     Quadrant3,
  8.     Quadrant4
  9. }
  10.   
  11. [ExecuteInEditMode]
  12. [AddComponentMenu("Auto Layout #0")]
  13. public class AutoLayout  : MonoBehaviour{
  14.     Camera uiCamera;
  15.       
  16.     QuadrantLayout quadrant;
  17.     Vector2 margin;
  18.       
  19.     Vector3 lastPos;
  20.     void OnEnable (){
  21.         if (uiCamera == null) uiCamera = Camera.main;
  22.         if(uiCamera == null){
  23.             Debug.LogWarning("The uiCamera must not be null!");
  24.             this.enabled = false;
  25.             return;
  26.         }
  27. #if !UNITY_EDITOR
  28.         redressTransform();
  29. #endif
  30.     }
  31.       
  32.     Rect mRect;
  33.     float width,height;
  34.     Vector2 center,vcenter;
  35.     Vector3 v,sv;
  36. #if UNITY_EDITOR
  37.     void Update(){
  38.         if(uiCamera==null){
  39.             uiCamera = Camera.main;
  40.         }
  41.           
  42.         if(Vector3.Distance(transform.position,lastPos)>0.001f){//editor
  43.             updateMarginOffset();
  44.         }else{
  45.             redressTransform();
  46.         }
  47.         lastPos = transform.position;
  48.     }
  49. #endif
  50.       
  51.     void updateMarginOffset(){
  52.         if(uiCamera == null)return;
  53.           
  54.         mRect = uiCamera.pixelRect;
  55.         center = new Vector2((mRect.xMin + mRect.xMax) * 0.5f,(mRect.yMin + mRect.yMax) * 0.5f);
  56.         width = mRect.width;
  57.         height = mRect.height;
  58.           
  59.         //update offset
  60.         v = uiCamera.WorldToScreenPoint(transform.position);
  61.           
  62.         if(v.x>=center.x&&v.y>=center.y){//1
  63.             vcenter = new Vector2(width,height);
  64.             quadrant = QuadrantLayout.Quadrant1;
  65.         }else if(v.x>=center.x&&v.y<center.y){//2
  66.             vcenter = new Vector2(width,0);
  67.             quadrant = QuadrantLayout.Quadrant2;
  68.         }else if(v.x<center.x&&v.y<center.y){//3
  69.             vcenter = new Vector2(0,0);
  70.             quadrant = QuadrantLayout.Quadrant3;
  71.         }else if(v.x<center.x&&v.y>=center.y){//4
  72.             vcenter = new Vector2(0,height);
  73.             quadrant = QuadrantLayout.Quadrant4;
  74.         }
  75.         margin = new Vector2((v.x-vcenter.x),(v.y-vcenter.y));
  76.     }
  77.       
  78.     void redressTransform(){
  79.         if(uiCamera == null)return;
  80.         mRect = uiCamera.pixelRect;
  81.         width = mRect.width;
  82.         height = mRect.height;
  83.           
  84.         switch(quadrant){
  85.         case QuadrantLayout.Quadrant1:
  86.             sv = new Vector3(width+margin.x,height+margin.y,0);
  87.             break;
  88.         case QuadrantLayout.Quadrant2:
  89.             sv = new Vector3(width+margin.x,margin.y,0);
  90.             break;
  91.         case QuadrantLayout.Quadrant3:
  92.             sv = new Vector3(margin.x,margin.y,0);
  93.             break;
  94.         case QuadrantLayout.Quadrant4:
  95.             sv = new Vector3(margin.x,height+margin.y,0);
  96.             break;
  97.         }
  98.         sv.z = uiCamera.WorldToScreenPoint(transform.position).z;
  99.         sv = uiCamera.ScreenToWorldPoint(sv);
  100.         if (uiCamera.orthographic){
  101.             sv.x = Mathf.Round(sv.x);
  102.             sv.y = Mathf.Round(sv.y);
  103.         }
  104.         transform.position = sv;
  105.     }
  106. }

根据象限来划分UI的位置,然后在不同分辨率自动调整,不用设置任何属性,只要把脚本附加到物体上就可以。

以上就是Unity 窗口适配的方案,但是有一点,代码中是默认使用tag为Main Camera的相机,如果场景中有多个相机,那么要保证UI的相机被标记了Main Camera.