这个适配是依据坐标系的象限的思想来进项适配的。參考了部分的NGUI的适配方案。

在程序的事实上,来測量UI距离相机边界的像素然后依据比例来进行适配,个人认为还不错。 放码!

有个前提哦就是你要先定一个尺寸。

假如我优先适配1024*768。那在放置这个脚本之前,要把自己的界面还成1024*768的哦。我是依据第一次来进行适配的哦。

using UnityEngine;
using System.Collections;
#if UNITY_EDITOR
using UnityEditor;
#endif public enum QuadrantLayout{
Quadrant1,
Quadrant2,
Quadrant3,
Quadrant4
} [ExecuteInEditMode]
public class AutoLayout : MonoBehaviour{ [HideInInspector] public QuadrantLayout quadrant;
[HideInInspector] public Vector2 margin;
Vector3 lastPostion; #if UNITY_EDITOR
[HideInInspector] [SerializeField] private bool isFirstLoad=true;
int pixelWidth;
void Awake(){
lastPostion = this.transform.localPosition;
if(isFirstLoad){
updateMarginOffset();
isFirstLoad=false;
}else{
resetMarginOffset();
}
} void Update(){
if(!Application.isPlaying){
if(Vector3.Distance(lastPostion,this.transform.localPosition)>0.001f && Selection.activeGameObject == this.gameObject){
updateMarginOffset();
UnityEditor.EditorUtility.SetDirty(this);
}else{
resetMarginOffset();
}
lastPostion = this.transform.localPosition;
}else{
if((int)Camera.main.pixelWidth!=pixelWidth){
resetMarginOffset();
}
pixelWidth = (int)Camera.main.pixelWidth;
}
}
#else
void Start(){
resetMarginOffset();
}
#endif void updateMarginOffset(){
float m = (Camera.main.WorldToScreenPoint(new Vector3(1,0,0))-Camera.main.WorldToScreenPoint(Vector3.zero)).x;
float halfWidth=Camera.main.pixelWidth/2.0f/m;
float halfHeight=Camera.main.pixelHeight/2.0f/m;
Vector3 v = this.transform.position; //1
if(v.x>=0 && v.y>=0){
quadrant = QuadrantLayout.Quadrant1;
margin = new Vector2(halfWidth-v.x,halfHeight-v.y);
//2
}else if(v.x>=0 && v.y<=0){
quadrant = QuadrantLayout.Quadrant2;
margin = new Vector2(halfWidth-v.x,halfHeight+v.y);
//3
}else if(v.x<=0 && v.y<=0){
quadrant = QuadrantLayout.Quadrant3;
margin = new Vector2(halfWidth+v.x,halfHeight+v.y);
//4
}else if(v.x<=0 && v.y>=0){
quadrant = QuadrantLayout.Quadrant4;
margin = new Vector2(halfWidth+v.x,halfHeight-v.y);
} } void resetMarginOffset(){
Vector3 sv = Vector3.zero; float m = (Camera.main.WorldToScreenPoint(new Vector3(1,0,0))-Camera.main.WorldToScreenPoint(Vector3.zero)).x;
float halfWidth=Camera.main.pixelWidth/2.0f/m;
float halfHeight=Camera.main.pixelHeight/2.0f/m; switch(quadrant){
case QuadrantLayout.Quadrant1:
sv = new Vector3(halfWidth-margin.x,halfHeight-margin.y,0);
break;
case QuadrantLayout.Quadrant2:
sv = new Vector3(halfWidth-margin.x,margin.y-halfHeight,0);
break;
case QuadrantLayout.Quadrant3:
sv = new Vector3(margin.x-halfWidth,margin.y-halfHeight,0);
break;
case QuadrantLayout.Quadrant4:
sv = new Vector3(margin.x-halfWidth,halfHeight-margin.y,0);
break;
} sv.z = this.transform.position.z; transform.position = sv;
}
}

最新文章

  1. 工作流引擎Oozie(一):workflow
  2. ES5基础之正则表达式01:初次见面
  3. Java Thread 多线程 介绍
  4. html table表头斜线
  5. js的事件委托
  6. 找一个四位数,要求该四位数的四倍刚好是该四位数的反序。 即b1b2b3b4 * 4 = b4b3b2b1
  7. grep -w
  8. TYVJ P1031 热浪 Label:dijkstra 最短路
  9. hdu 1429 胜利大逃亡(续) (bfs+状态压缩)
  10. Fixing:insert_modules not found
  11. 装有Win7系统的电脑在局域网不能共享的解决方案
  12. 数据库 —— mySQL的基本操作
  13. Margin是什么?
  14. Java泛型解析(03):虚拟机运行泛型代码
  15. 在 Tomcat 上配置虚拟主机
  16. J2SE学习笔记
  17. map-有序 multimap-可重复 unordered_map-无序
  18. vue项目中编写一个图片预览的公用组件
  19. shell脚本中的set -e和set -o pipefail
  20. 7.26-Codeforces Round #372 (Div. 2)

热门文章

  1. ASP.NET MVC 视图学习,纯干货
  2. CentOS7.5更改grub2菜单背景&amp;开机动态画面
  3. Vue生命周期总结
  4. react native 手势响应
  5. thinkjphp 模板中获取url中的action
  6. 洛谷 P3384 【模板】树链剖分-树链剖分(点权)(路径节点更新、路径求和、子树节点更新、子树求和)模板-备注结合一下以前写的题目,懒得写很详细的注释
  7. Calendar类进行日期操作
  8. CentOS7终端如何支持中文显示
  9. 【大杀器】利用划分树秒杀区间内第k大的数
  10. CSU - 1337 (搞笑版费马大定理 )