转载链接:
https://blog.csdn.net/cqwlxu/article/details/105869877
RectTransform .sizeDelta :此RectTransform的大小相对于锚点之间的距离。
sizeDelta=此RectTransform矩阵大小-锚点大小
-
public Vector2 SizeDelta()
-
{
-
RectTransform parent = transform.parent as RectTransform;
-
RectTransform child = transform as RectTransform;
-
//sizeDelta = rect.size-(anchorMax-acchorMin)* parent.rect.size(注意 这里不是点乘)
-
Vector2 anchorDistances = (child.anchorMax - child.anchorMin) * parent.rect.size;
-
Vector2 sizeDelta = rectTransform.rect.size - anchorDistances;
-
return sizeDelta;
-
}
举个例子:
黄色是绿色的父级。
绿色的长度-锚点的长度
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();