//1.屏幕转世界坐标
Vector3 Camera.main.ScreenToWorldPoint();
//2.世界转屏幕坐标
Vector3 Camera.main.WorldToScreenPoint();
//3.世界转视口坐标
Vector3 Camera.main.WorldToViewportPoint();
//4.视口转世界坐标
Vector3 Camera.main.ViewportToWorldPoint();
//5.视口转屏幕坐标
Vector3 Camera.main.ViewportToScreenPoint();
//6.屏幕转视口坐标
Vector3 Camera.main.ScreenToViewportPoint();
//6.屏幕转UI坐标
Vector3 WordTOScreer(Vector3 pos)
{
Vector2 aimScreenPos = Camera.main.WorldToScreenPoint(pos);
//在将屏幕坐标转换为UGUI坐标:
Vector2 aimLocalPos;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(transform as RectTransform, aimScreenPos, Camera.main, out aimLocalPos))
{
// Debug.Log(aimLocalPos);
return aimLocalPos;
}
return Vector3.zero;
}
//unity中Canvas改为Screen Space-Camera模式下鼠标拖动物体
private void Update()
{
if (_trans == null && Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (hit.transform.CompareTag("Turret"))
{
_trans = hit.transform;
}
}
}
if (_trans != null)
{
_vec3TargetScreenSpace = Camera.main.WorldToScreenPoint(_trans.position);
_vec3MouseScreenSpace = Input.mousePosition;
_vec3MouseScreenSpace.z = _vec3TargetScreenSpace.z;
_vec3TargetWorldSpace = Camera.main.ScreenToWorldPoint(_vec3MouseScreenSpace);
_trans.position = new Vector3(_vec3TargetWorldSpace.x, 2.3f, _vec3TargetWorldSpace.z);
}
}
这些都是踩坑过来的,一点点的进步,慢慢完善,多多指教!