
Unity使用GUI做屏幕自适应(一)
发布日期:2021-05-08 16:29:38
浏览次数:22
分类:精选文章
本文共 4075 字,大约阅读时间需要 13 分钟。
屏幕自适应
文章目录
前言
适应不同屏幕的分辨率,本文采用九宫格方法
一、九宫格
二、创建位置信息类
代码如下(示例):
public enum E_Alignment_Type{ Up, Down, Left, Right, Center, Left_Up, Left_Down, Right_Up, Right_Down,}////// 该类用来表示位置 计算位置相关信息的/// public class CustomGUIPos{ //处理控件位置相关内容 //完成 分辨率自适应的相关计算 //该位置信息 会用来返回给外部 用于绘制控件 //需要对它进行 计算 private Rect rPos = new Rect(0, 0, 100, 100); //屏幕九宫格对齐方式 public E_Alignment_Type screnn_Alignment_Type; //控件中心对齐方式 public E_Alignment_Type control_Alignment_Type; //偏移位置 public Vector2 pos; //宽高 public float width = 100; public float height = 50; //用于计算的中心点成员变量 private Vector2 centerPos; //计算中心点偏移的方法 private void CalcCenterPos() { switch (control_Alignment_Type) { case E_Alignment_Type.Up: centerPos.x = -width / 2; centerPos.y = 0; break; case E_Alignment_Type.Down: centerPos.x = -width / 2; centerPos.y = -height; break; case E_Alignment_Type.Left: centerPos.x = 0; centerPos.y = -height / 2; break; case E_Alignment_Type.Right: centerPos.x = -width; centerPos.y = -height / 2; break; case E_Alignment_Type.Center: centerPos.x = -width / 2; centerPos.y = -height / 2; break; case E_Alignment_Type.Left_Up: centerPos.x = 0; centerPos.y = 0; break; case E_Alignment_Type.Left_Down: centerPos.x = 0; centerPos.y = -height; break; case E_Alignment_Type.Right_Up: centerPos.x = -width; centerPos.y = 0; break; case E_Alignment_Type.Right_Down: centerPos.x = -width; centerPos.y = -height; break; } } //计算最终相对坐标位置的方法 private void CalcPos() { switch (screnn_Alignment_Type) { case E_Alignment_Type.Up: rPos.x = Screen.width / 2 + centerPos.x + pos.x; rPos.y = 0 + centerPos.y + pos.y; break; case E_Alignment_Type.Down: rPos.x = Screen.width / 2 + centerPos.x + pos.x; rPos.y = Screen.height + centerPos.y - pos.y; break; case E_Alignment_Type.Left: rPos.x = 0 + centerPos.x + pos.x; rPos.y = Screen.height / 2 + centerPos.y + pos.y; break; case E_Alignment_Type.Right: rPos.x = Screen.width + centerPos.x - pos.x; rPos.y = Screen.height / 2 + centerPos.y + pos.y; break; case E_Alignment_Type.Center: rPos.x = Screen.width / 2 + centerPos.x + pos.x; rPos.y = Screen.height / 2 + centerPos.y + pos.y; break; case E_Alignment_Type.Left_Up: rPos.x = 0 + centerPos.x + pos.x; rPos.y = 0 + centerPos.y + pos.y; break; case E_Alignment_Type.Left_Down: rPos.x = 0 + centerPos.x + pos.x; rPos.y = Screen.height + centerPos.y - pos.y; break; case E_Alignment_Type.Right_Up: rPos.x = Screen.width + centerPos.x - pos.x; rPos.y = 0 + centerPos.y + pos.y; break; case E_Alignment_Type.Right_Down: rPos.x = Screen.width + centerPos.x - pos.x; rPos.y = Screen.height + centerPos.y - pos.y; break; } } ////// 得到最终位置和宽高 /// public Rect Pos { get { //进行计算 CalcCenterPos();//计算中心点偏移 CalcPos();//计算相对屏幕坐标点 //宽高直接赋值 返回给外部 别人直接使用来绘制控件 rPos.width = width; rPos.height = height; return rPos; } }}
总结
本脚本完成了GUI屏幕坐标的相关计算
发表评论
最新留言
逛到本站,mark一下
[***.202.152.39]2025年04月05日 03时30分17秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
MSSQL 2005 数据库变成可疑状态
2021-05-09
QBlog V2.5 源码开放下载(ASP.NET 番外系列之开端)
2021-05-09
秋色园引发CPU百分百命案的事件分析与总结
2021-05-09
安装jdk并配置环境变量
2021-05-09
稀疏数组
2021-05-09
js的严格模式
2021-05-09
idea的安装和无限期试用
2021-05-09
Oracle VM VirtualBox安装PVE虚拟机
2021-05-09
【转】如何用css限制文字长度,使溢出的内容用省略号…显示
2021-05-09
Android MediaPlayer setDataSource failed
2021-05-09
ASP.NET Core 实战:Linux 小白的 .NET Core 部署之路
2021-05-09
【nodejs原理&源码杂记(8)】Timer模块与基于二叉堆的定时器
2021-05-09
大前端的自动化工厂(1)——Yeoman
2021-05-09
数据仓库建模方法论
2021-05-09
虚拟机搭建hadoop环境
2021-05-09
DataStax Bulk Loader教程(四)
2021-05-09
.NET应用框架架构设计实践 - 概述
2021-05-09
Rust 内置 trait :PartialEq 和 Eq
2021-05-09
Hibernate(十四)抓取策略
2021-05-09