RectTransformUtility.ScreenPointToWorldPointInRectangle使用说明

首先,看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

posted @ 2018-07-22 22:09  EkkoMay  阅读(3300)  评论(0)    收藏  举报