获取UI(RectTransform)四个角的屏幕坐标
Vector3[] corners = new Vector3[4];
private Vector3 GetCornerScreenPos(RectTransform rt, int index)
{
rt.GetWorldCorners(corners);
Vector3 wPos = corners[index];
#if UNITY_EDITOR
Debug.Log($"{rt.name}:wPos:{wPos}");
#endif
var UICanvas =rt.GetComponentInParent<Canvas>();
Camera uCame = UICanvas.worldCamera;
Vector3 screenPos = wPos;
if (UICanvas.renderMode != RenderMode.ScreenSpaceOverlay && uCame !=null)
{
screenPos = uCame.WorldToScreenPoint(wPos);
}
#if UNITY_EDITOR
Debug.Log($"{rt.name}:screenPos:{screenPos}");
#endif
return screenPos;
}