一些关于坐标的转换

世界空间中的点坐标转换到屏幕坐标:

screenPos = RectTransformUtility.WorldToScreenPoint(cam, worldPos.transform.position);

UGUI物体的坐标转换到屏幕坐标:

screenPos = RectTransformUtility.WorldToScreenPoint(canvas.worldCamera, uguiObj.transform.position);

屏幕坐标转换到UGUI坐标:

Vector3 worldPoint;
if (RectTransformUtility.ScreenPointToWorldPointInRectangle(rectTrans, camPos, canvas.worldCamera,out worldPoint))
{
  transform.position = worldPoint;
}

屏幕坐标转换到世界空间坐标(射线碰撞位置):

var ray = RectTransformUtility.ScreenPointToRay(worldCamera, screenPos);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo))
{
  pos.transform.position = hitInfo.point;
}

猜你喜欢

转载自blog.csdn.net/lvcoc/article/details/85235359