关于RectTransform.sizeDelta

转载链接:

https://blog.csdn.net/cqwlxu/article/details/105869877

RectTransform .sizeDelta :此RectTransform的大小相对于锚点之间的距离。

sizeDelta=此RectTransform矩阵大小-锚点大小

  1. public Vector2 SizeDelta()

  2. {

  3. RectTransform parent = transform.parent as RectTransform;

  4. RectTransform child = transform as RectTransform;

  5. //sizeDelta = rect.size-(anchorMax-acchorMin)* parent.rect.size(注意 这里不是点乘)

  6. Vector2 anchorDistances = (child.anchorMax - child.anchorMin) * parent.rect.size;

  7. Vector2 sizeDelta = rectTransform.rect.size - anchorDistances;

  8. return sizeDelta;

  9. }

举个例子:

黄色是绿色的父级。

绿色的长度-锚点的长度

sizeDelta.x=100-(0.5-0)*300=-50; 

sizeDelta.y=50-(0.5-0.5)*200=50;

可见,锚点y重合时,最小的y和最大的y一样 此时锚点Y距离为0(yMax-yMin),也就是本身矩阵的高度。

这就是为什么 4个锚点重合是, sizeDelta和rect.size一样大小。

第一次见到是百度修改UI大小时,直接一行 RectTransform.sizeDelta = new Vector2(x,y);

锚点分离是不好用,这里建议使用 RectTransform.SetSizeWithCurrentAnchors();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值