public static bool ScreenPointToWorldPointInRectangle(RectTransform rect, Vector2 screenPoint, Camera cam, out Vector3 worldPoint) { worldPoint = (Vector3)Vector2.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); }
public static bool ScreenPointToWorldPointInRectangle(RectTransform rect, Vector2 screenPoint, Camera cam, out Vector3 worldPoint) { worldPoint = Vector2.zero; Ray ray = RectTransformUtility.ScreenPointToRay(cam, screenPoint); Plane plane = new Plane(rect.rotation * Vector3.back, rect.position); float distance; bool result; if (!plane.Raycast(ray, out distance)) { result = false; } else { worldPoint = ray.GetPoint(distance); result = true; } return(result); }