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);