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);
        }
    }

你可能感兴趣的:(unity3d)