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;
}
打印可以看出两个坐标是相等的。