我想要确定在Unity3D中RectTransform的屏幕坐标。考虑到元素可能会被锚定或缩放,应该如何完成此操作?
我试图使用RectTransform.GetWorldCorners,然后将每个Vector3转换为屏幕坐标,但是值是错误的。
public Rect GetScreenCoordinates(RectTransform uiElement)
{
var worldCorners = new Vector3[4];
uiElement.GetWorldCorners(worldCorners);
var result = new Rect(
worldCorners[0].x,
worldCorners[0].y,
worldCorners[2].x - worldCorners[0].x,
worldCorners[2].y - worldCorners[0].y);
for (int index = 0; index < 4; index++)
result[index] = Camera.main.WorldToScreenPoint(result[index]);
return result;
}