世界空间中的点坐标转换到屏幕坐标:
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;
}
计算鼠标点击位置到锚点位置的偏差:
Vector3 globalMousePos;
RectTransformUtility.ScreenPointToWorldPointInRectangle(m_DraggingPlane, Input.mousePosition,
eventData.pressEventCamera, out globalMousePos);
offset = transform.position - globalMousePos;
拖动时:
Vector3 globalMousePos;
if (RectTransformUtility.ScreenPointToWorldPointInRectangle(m_DraggingPlane, data.position, data.pressEventCamera, out globalMousePos))
{
transform.position = globalMousePos + offset;
transform.rotation = m_DraggingPlane.rotation;
}