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