UGUI RectTransform 矩形变换

朋丶Peng

天道酬勤!

UGUI RectTransform 矩形变换

UGUI游戏对象基本都有这个组件。

float radius;
radius = GetComponent<RectTransform>().sizeDelta.x;
radius = (transform as RectTransform).sizeDelta.xf;

上面两个给radius赋值的方式是一样的.

UGUI游戏对象的 RectTransform.position  与 transform.position ,RectTransform.localPosition  与 transform.localPosition ,它们是一样的。

下面是RectTransform的一些变量,可以Unity圣典里面查看:

Variables 变量:

anchoredPosition The position of the pivot of this RectTransform relative to the anchor reference point.
该矩形变换相对于锚点参考点的中心点位置。
anchoredPosition3D The 3D position of the pivot of this RectTransform relative to the anchor reference point.
该矩阵变换相对于锚点参考点的中心点的3D位置。
anchorMax The normalized position in the parent RectTransform that the upper right corner is anchored to.
该锚点在父矩阵变换中归一化位置,右上角是锚点。
anchorMin The normalized position in the parent RectTransform that the lower left corner is anchored to.
在父矩阵变换上归一化位置,该锚点在左下角。
offsetMax The offset of the upper right corner of the rectangle relative to the upper right anchor.
矩形右上角相对于右上角锚点的偏移量。
offsetMin The offset of the lower left corner of the rectangle relative to the lower left anchor.
矩形左下角相对于左下角锚点的偏移量。
pivot The normalized position in this RectTransform that it rotates around.
在该矩阵变换的归一化位置,围绕该中心点旋转。
rect The calculated rectangle in the local space of the Transform.
计算矩形自身空间的变换。
sizeDelta The size of this RectTransform relative to the distances between the anchors.
矩阵变换的大小相对于锚点之间的距离。

用代码去更改:

1.改变RectTransform的top

GetComponent<RectTransform>().offsetMax = new Vector2(GetComponent<RectTransform>().offsetMax.x, top);

2.改变RectTransform的bottom

GetComponent<RectTransform>().offsetMin = new Vector2(GetComponent<RectTransform>().offsetMin.x, bottom);

3.改变RectTransform的width,height

GetComponent<RectTransform>().sizeDelta = new Vector2(width, height);

4.改变RectTransform的pos

GetComponent<RectTransform>().anchoredPosition3D = new Vector3(posx,posy,posz);

GetComponent<RectTransform>().anchoredPosition = new Vector2(posx,posy);

猜你喜欢

转载自blog.csdn.net/qq2512667/article/details/81713857