Unity RectTransform中使用stretch模式时代码动态控制Left、Top、Right、Bottom

发布时间 2023-10-16 16:23:33作者: 威少小二orz

Unity RectTransform中使用stretch模式时代码动态控制Left、Top、Right、Bottom

对于屏幕进行适应的代码:

  var rect = panelObj.AddComponent<RectTransform>();
            rect.anchorMin = Vector2.zero;
            rect.anchorMax = Vector2.one;
            rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, Screen.width);
            rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, Screen.height);

对于Left、Top、Right、Bottom进行修改:

            rect.offsetMin = Vector2.zero;//对应Left、Buttom
            rect.offsetMax = Vector2.zero;//对应Right、Top