首先,看Unity Scripting API上的介紹:
public static bool ScreenPointToWorldPointInRectangle(RectTransform rect, Vector2 screenPoint, Camera cam, out Vector3 worldPoint);
描述:
將屏幕空間上的點轉換為位於給定RectTransform平面上的世界空間中的位置。cam參數應該是與屏幕點相關的相機。對於Canvas設置為“Screen Space - Overlay mode”模式的情況,cam參數應該為null。
示例:Vector2 position;
RectTransformUtility.ScreenPointToWorldPointInRectangle(GameObject.Find("Canvas").transform as RectTransform,Input.GetmousePosition,null,out positoin) // 將當前所在像素坐標的鼠標位置轉換為Canvas物體所在平面上的世界空間中的位置,將位置信息返回給position