- 如何获取UI控件的大小(屏幕坐标系) ?
GetComponent<RectTransform>().rect
rect是一个四维向量只读属性,前两维是Pivot到控件所占矩形左下角点的距离,后两维分别为控件的宽和高
其他RectTransform类主要属性
- sizeDelta (Vector2)
Anchors矩形与控件实际所占矩形面积的宽和高的差值,负数为控件比Anchors面积小
- anchoredPosition3D (Vector3)
一个神奇的属性,pivot相对于Anchors在三维坐标系中的位置(If the anchors are not together, the four anchor positions are interpolated according to the pivot placement.)
一个方便查看RectTransform类内置public属性的脚本
using UnityEngine;
[RequireComponent(typeof(RectTransform))]
public class TestUI