Unity 窗口适配要怎么做呢?现在的设备比较多,尤其是安卓,光手机可能就有好几种不同的分辨率,更不要说平板了,虽然苹果分辨率就那么几个,但是从出了6以后,苹果的分辨率也比较多了,下面我就分享一下Unity屏幕适配方案供大家参考学习。
unity中屏幕适配一直都没有一个完美的解决方案,在这里写了一个编辑器模式运行的类,可以在编辑器模式下方便的调整好UI位置然后在运行时候自动对齐。
具体代码实现如下AutoLayout.cs:
- using UnityEngine;
- using System.Collections;
- public enum QuadrantLayout{
- Quadrant1,
- Quadrant2,
- Quadrant3,
- Quadrant4
- }
- [ExecuteInEditMode]
- [AddComponentMenu("Auto Layout #0")]
- public class AutoLayout : MonoBehaviour{
- Camera uiCamera;
- QuadrantLayout quadrant;
- Vector2 margin;
- Vector3 lastPos;
- void OnEnable (){
- if (uiCamera == null) uiCamera = Camera.main;
- if(uiCamera == null){
- Debug.LogWarning("The uiCamera must not be null!");
- this.enabled = false;
- return;
- }
- #if !UNITY_EDITOR
- redressTransform();
- #endif
- }
- Rect mRect;
- float width,height;
- Vector2 center,vcenter;
- Vector3 v,sv;
- #if UNITY_EDITOR
- void Update(){
- if(uiCamera==null){
- uiCamera = Camera.main;
- }
- if(Vector3.Distance(transform.position,lastPos)>0.001f){//editor
- updateMarginOffset();
- }else{
- redressTransform();
- }
- lastPos = transform.position;
- }
- #endif
- void updateMarginOffset(){
- if(uiCamera == null)return;
- mRect = uiCamera.pixelRect;
- center = new Vector2((mRect.xMin + mRect.xMax) * 0.5f,(mRect.yMin + mRect.yMax) * 0.5f);
- width = mRect.width;
- height = mRect.height;
- //update offset
- v = uiCamera.WorldToScreenPoint(transform.position);
- if(v.x>=center.x&&v.y>=center.y){//1
- vcenter = new Vector2(width,height);
- quadrant = QuadrantLayout.Quadrant1;
- }else if(v.x>=center.x&&v.y<center.y){//2
- vcenter = new Vector2(width,0);
- quadrant = QuadrantLayout.Quadrant2;
- }else if(v.x<center.x&&v.y<center.y){//3
- vcenter = new Vector2(0,0);
- quadrant = QuadrantLayout.Quadrant3;
- }else if(v.x<center.x&&v.y>=center.y){//4
- vcenter = new Vector2(0,height);
- quadrant = QuadrantLayout.Quadrant4;
- }
- margin = new Vector2((v.x-vcenter.x),(v.y-vcenter.y));
- }
- void redressTransform(){
- if(uiCamera == null)return;
- mRect = uiCamera.pixelRect;
- width = mRect.width;
- height = mRect.height;
- switch(quadrant){
- case QuadrantLayout.Quadrant1:
- sv = new Vector3(width+margin.x,height+margin.y,0);
- break;
- case QuadrantLayout.Quadrant2:
- sv = new Vector3(width+margin.x,margin.y,0);
- break;
- case QuadrantLayout.Quadrant3:
- sv = new Vector3(margin.x,margin.y,0);
- break;
- case QuadrantLayout.Quadrant4:
- sv = new Vector3(margin.x,height+margin.y,0);
- break;
- }
- sv.z = uiCamera.WorldToScreenPoint(transform.position).z;
- sv = uiCamera.ScreenToWorldPoint(sv);
- if (uiCamera.orthographic){
- sv.x = Mathf.Round(sv.x);
- sv.y = Mathf.Round(sv.y);
- }
- transform.position = sv;
- }
- }
根据象限来划分UI的位置,然后在不同分辨率自动调整,不用设置任何属性,只要把脚本附加到物体上就可以。
以上就是Unity 窗口适配的方案,但是有一点,代码中是默认使用tag为Main Camera的相机,如果场景中有多个相机,那么要保证UI的相机被标记了Main Camera.