GUI实现九宫格来完成屏幕自适应

我们把屏幕看成是一个九宫格的话可以分为左上,中上,右上,左,中,右,左下,中下,右下

把一个控件比如一张图片也可以这么分。

屏幕的锚点加上控件的轴心点加上偏移量即可实现控件在不同屏幕大小的位置自适应。

设置位置的九宫格可以使用一个类来设置相关的信息

类中的成员变量包括,私有的位置信息,公共的设置宽的变量,公共的设置高的变量,公共的设置偏移量的变量2个枚举变量,

成员属性包括得到位置的属性(只可以得不可以改),在得到属性的同时计算出位置的具体的信息

类中的成员方法包括,设置关于控件的位置,设置关于屏幕的位置加上控件的位置加上偏移量

public enum posDirection
{
    leftUp,
    middleUp,
    rightUp,
    left,
    middle,
    right,
    leftDown,
    middleDown,
    rightDown

    }
[System.Serializable]//利用特性结合反射来实现在类中作为类成员时可以显示出该类的公共成员在inspector面板上
public class uiPositions
{
    //数据类,不需要继承monobehaviour,后面作为类对象来得到公共的属性来计算和得出控件的位置
    
    Rect rPos = new Rect(0, 0, 100, 100);
    //偏移信息
    //偏移信息设置为公有的,外部可以设置
    public Vector2 Dire;
    public float width=100;//控件的宽
    public float height=50;//控件的高
    public posDirection ScreenPos = posDirection.middle;
    public posDirection ControllerPos = posDirection.middle;
    
    //设置轴心点
    public void SetControllerPos()
    {
        switch (ControllerPos)
        {
            case posDirection.leftUp:
             
                rPos.x = 0;
                rPos.y = 0;
                break;
            case posDirection.middleUp:
              
                rPos.x = -(width / 2);
                rPos.y = 0;
                break;
            case posDirection.rightUp:
                rPos.x = -width;
                rPos.y = 0;
                break;
            case posDirection.left:
                rPos.x = 0;
                rPos.y = -(height / 2);

                break;
            case posDirection.middle:
                rPos.x = -(width / 2);
                rPos.y = -(height / 2);
                break;
            case posDirection.right:
                rPos.x = -width;
                rPos.y = -(height / 2);
                break;
            case posDirection.leftDown:
                rPos.x = 0;
                rPos.y = -height;
                break;
            case posDirection.middleDown:
                rPos.x = -(width / 2);
                rPos.y = -height;
                break;
            case posDirection.rightDown:
                rPos.x = -width;
                rPos.y = -height;
                break;

        }
    }
    public void SetAllPos()
    {
        switch (ScreenPos)
        {
            case posDirection.leftUp:
                rPos.x += Dire.x;
                rPos.y += Dire.y;
                break;
            case posDirection.middleUp:
                rPos.x += Screen.width / 2+Dire.x;
                rPos.y += Dire.y;
                break;
            case posDirection.rightUp:
                rPos.x += Screen.width - Dire.x;
                rPos.y += Dire.y;
                break;
            case posDirection.left:
                rPos.x += Dire.x;
                rPos.y += Screen.height / 2 + Dire.y;


                break;
            case posDirection.middle:
                rPos.x += Screen.width / 2 + Dire.x;
                rPos.y += Screen.height / 2 + Dire.y;
                break;
            case posDirection.right:
                rPos.x +=Screen.width-Dire.x;
                rPos.y += Screen.height /2+ Dire.y;
                break;
            case posDirection.leftDown:
                rPos.x += Dire.x;
                rPos.y += Screen.height - Dire.y;

                break;
            case posDirection.middleDown:
                rPos.x += Screen.width / 2 + Dire.x;
                rPos.y += Screen.height - Dire.y;
                break;
            case posDirection.rightDown:
                rPos.x += Screen.width  - Dire.x;
                rPos.y += Screen.height - Dire.y;
                break;

        }
    }
//设置属性来只可以得到不可以改变
    public Rect Pos
    {
        get
        {
            rPos.width = width;
            rPos.height = height;
            SetControllerPos();
            SetAllPos();
            return rPos;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值