一个简单的获得点击世界坐标的方法

1
2
3
4
5
6
7
8
9
10
11
public bool ScreenPoitToWorldPointInRectangle(RectTransform rect, Vector2 screenPoint, Camera cam, out Vector3 worldPoint) {
worldPoint = Vector3.zero;
Ray ray = RectTransformUtility.ScreenPointToRay(cam, screenPoint);
float enter;
if(!new Plane(rect.rotation * Vector3.back, rect.position).Raycast(ray, out enter)) {
return false;
}

worldPoint = ray.GetPoint(enter);
return true;
}

要再获得ui的本地坐标的话就rectTransform.InverseTransformPoint(worldPoint)