Unity使用GUI做屏幕自适应(一)
发布日期:2021-05-08 16:29:38 浏览次数:22 分类:精选文章

本文共 4075 字,大约阅读时间需要 13 分钟。

屏幕自适应


文章目录


前言

适应不同屏幕的分辨率,本文采用九宫格方法UML类图分析


一、九宫格

在这里插入图片描述

二、创建位置信息类

代码如下(示例):

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屏幕坐标的相关计算

上一篇:Unity使用GUI做屏幕自适应(完)
下一篇:Unity之PlayerPrefs

发表评论

最新留言

逛到本站,mark一下
[***.202.152.39]2025年04月05日 03时30分17秒