RectTransformUtility.ScreenPointToLocalPointInRectangle(rRect, rPosition, UICamera, out Vector2 otherPoint);
要替换这个。
private Vector3 ScreenPointToLocalPointInRectangle(RectTransform rRect,Vector2 rPosition,Camera rCamera)
{
Vector3 lPosition = UICamera.ScreenToWorldPoint(rPosition);
Vector4 l4Position = new Vector4(lPosition.x,lPosition.y,lPosition.z,1); // unity默认转化v4,w是0,不会计算x和y的rect的偏移,所以这里要自己搞一下。
lPosition = rRect.worldToLocalMatrix * l4Position;
//Debug.LogError($"{rRect.name} - {rRect.worldToLocalMatrix}");
//lPosition = rRect.InverseTransformPoint(lPosition);
RectTransformUtility.ScreenPointToLocalPointInRectangle(rRect, rPosition, UICamera, out Vector2 otherPoint);
Debug.LogError($"{rRect.name} - {lPosition}");
Debug.LogError($"{rRect.name} - OtherPosition : {otherPoint}");
//Debug.LogError($"3 - {rRect.InverseTransformPoint(UICamera.ScreenToWorldPoint(rPosition))}");
return lPosition;
}
打印可以看出两个坐标是相等的。

这篇博客详细介绍了Unity中RectTransformUtility.ScreenPointToLocalPointInRectangle函数的使用,并提供了一个自定义实现的替代方法。作者通过分析源代码,展示了如何将屏幕坐标转换为RectTransform内的局部坐标,同时提供了调试信息以帮助理解转换过程。博客内容包括代码示例和调试日志,显示了两种方法得到的结果一致。
4万+

被折叠的 条评论
为什么被折叠?



