Unity3d UGUI 自适应屏幕 代码修改Left,Top,Right,Bottom属性值

通过RectTransform 的属性offsetMin,offsetMax来实现;

offsetMin   是vector2(left,bottom);

offsetMax 是vector2(right,top);//如果Anchors max=(1,1),right,top要传入负数;

例如:

private void AdaptUI() {
        if (m_left != null) {
            m_left.rectTransform.offsetMax = new Vector2(-(1.0f - x1) * Screen.width, 0);
        }
        if (m_top != null) {
            m_top.rectTransform.offsetMin = new Vector2(x1*Screen.width,y2* Screen.height);//offsetMin   left  bottom   ;  offsetmax    right top
            m_top.rectTransform.offsetMax = new Vector2(-(1.0f-x2)*Screen.width,0);
        }
        if (m_right != null) {
            m_right.rectTransform.offsetMin = new Vector2(x2*Screen.width,0);
        }
        if (m_bottom!=null) {
            m_bottom.rectTransform.offsetMin = new Vector2(x1*Screen.width,0);
            m_bottom.rectTransform.offsetMax = new Vector2(-(1.0f-x2)*Screen.width,-(1.0f-y1)*Screen.height-0.5f);
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值