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模式,也就是本文中的图二。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值