https://docs.unity.cn/cn/2019.2/ScriptReference/RectTransformUtility.ScreenPointToLocalPointInRectangle.html
用法:
Vector2 position;
RectTransformUtility.ScreenPointToLocalPointInRectangle(m_RectTransform, Input.mousePosition, null, out position);
public static bool ScreenPointToLocalPointInRectangle (RectTransform rect, Vector2 screenPoint, Camera cam, out Vector2 localPoint);
相机可以默认不填 ,这个相机指的是 渲染 canvas 的 相机,一般可以用 canvas.worldCamra来取到。
screenPoint一般指的就是 屏幕上的坐标 点, localpoint 是 转换出来的结果,因为vector2是 struct 值类型,因此要加个 out
另一个相关的是 屏幕坐标 转换成世间空间下 RectTransform 平面上的坐标。
public static bool ScreenPointToWorldPointInRectangle (RectTransform rect, Vector2 screenPoint, Camera cam, out Vector3 worldPoint);
用法是一样的,不过 放回的结果多了 z , 这个z的值的决定是 相机的坐标+ rect距离相机的 距离的值。 距离 正方向(相机面向 )时,为正,反方向(相机背向)时 为负。
https://docs.unity.cn/cn/2019.2/ScriptReference/RectTransformUtility.ScreenPointToWorldPointInRectangle.html