文章来源地址https://uudwc.com/A/yk4Gy文章来源:https://uudwc.com/A/yk4Gy
获取UI(RectTransform)四个角的屏幕坐标
Vector3[] corners = new Vector3[4];
/// <summary>
/// 左下、左上、右上、右下
/// </summary>
/// <param name="rt">ui</param>
/// <param name="index">0、1、2、3</param>
/// <returns></returns>
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;
}