这里先发布一个,自己写得unityUI的适配的方案(插播)
2024-09-01 13:27:59
这个适配是依据坐标系的象限的思想来进项适配的。參考了部分的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;
}
}
最新文章
- 工作流引擎Oozie(一):workflow
- ES5基础之正则表达式01:初次见面
- Java Thread 多线程 介绍
- html table表头斜线
- js的事件委托
- 找一个四位数,要求该四位数的四倍刚好是该四位数的反序。 即b1b2b3b4 * 4 = b4b3b2b1
- grep -w
- TYVJ P1031 热浪 Label:dijkstra 最短路
- hdu 1429 胜利大逃亡(续) (bfs+状态压缩)
- Fixing:insert_modules not found
- 装有Win7系统的电脑在局域网不能共享的解决方案
- 数据库 —— mySQL的基本操作
- Margin是什么?
- Java泛型解析(03):虚拟机运行泛型代码
- 在 Tomcat 上配置虚拟主机
- J2SE学习笔记
- map-有序 multimap-可重复 unordered_map-无序
- vue项目中编写一个图片预览的公用组件
- shell脚本中的set -e和set -o pipefail
- 7.26-Codeforces Round #372 (Div. 2)
热门文章
- ASP.NET MVC 视图学习,纯干货
- CentOS7.5更改grub2菜单背景&;开机动态画面
- Vue生命周期总结
- react native 手势响应
- thinkjphp 模板中获取url中的action
- 洛谷 P3384 【模板】树链剖分-树链剖分(点权)(路径节点更新、路径求和、子树节点更新、子树求和)模板-备注结合一下以前写的题目,懒得写很详细的注释
- Calendar类进行日期操作
- CentOS7终端如何支持中文显示
- 【大杀器】利用划分树秒杀区间内第k大的数
- CSU - 1337 (搞笑版费马大定理 )