Unity屏幕坐标转UI局部坐标

 Vector2 position;
        RectTransformUtility.ScreenPointToLocalPointInRectangle(m_RectTransform, Input.mousePosition, null, out  position);

https://docs.unity3d.com/ScriptReference/RectTransformUtility.ScreenPointToLocalPointInRectangle.html 附上官方 API

第一个参数是RectTransform 这个是 Cavas下的RectTransform 如果脚本挂载 在UI上 可以直接在Awake下用

 m_RectTransform = GetComponent();

 获取到。 第二个参数是要转换的坐标,screenPoint 屏幕坐标,也就是 鼠标的位置。第三个是cam ,如果 canvas模式设置为Screen-space camera模式的 话  这个cam就是第三个参数。有点像NGUI,第三个参数就是返回值, position ,

这个方法 就是 把屏幕上的一点 转换为UI RectTransform 的局部空间中的坐标。

RectTransform  矩阵变换工具

RectTransformUtility.ScreenPointToLocalPointInRectangle 从屏幕点到矩形内的本地点

你可能感兴趣的:(S3_Unity学习,UGUI)