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

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

在横向stretch、纵向stretch模式下:

 

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

            rect.offsetMin = Vector2.zero;//对应Left、Buttom  //new Vector2(x,y);,x对应Left,y对应Buttom
            rect.offsetMax = Vector2.zero;//对应Right、Top  //new Vector2(x,y);,x对应Right,y对应Top

 

在横向stretch、纵向top模式下:

 offsetMin对应Left、(-Height)

offsetMax对应-Right、PosY

 

在横向stretch、纵向middle械式下:

 offsetMin对应Left、(-Height)

offsetMax对应-Right、PosY

PosY会自动根据位置变化。

 

在横向stretch、纵向button模式下:

 

 offsetMin对应Left、(-Height)

offsetMax对应-Right、PosY

PosY会自动根据位置变化。

 

而上下滑动,一般用的最多的是:

在横向stretch、纵向top模式,也就是本文中的图二。

 

posted @ 2023-10-16 16:18  威少小二orz  阅读(381)  评论(0编辑  收藏  举报