一种在Unity中从世界坐标转UI坐标的方法

世界坐标→屏幕坐标→UI坐标

1
2
3
4
5
Vector3 screenPos = (Camera)mainCamera.WorldToScreenPoint(worldPos);
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(
(RectTransform)uiWindow.transform, screenPos, (Camera)uiCamera, out Vector2 localPoint)) {
transform.localPosition = localPoint + extraOffset + verticalOffset;
}