引用
https://blog.csdn.net/LLLLL__/article/details/88124564
各个坐标
世界坐标:世界中的坐标(transform.position)
局部坐标:相对父物体的坐标(transform.localPosition)
屏幕坐标:与屏幕的分辨率有关(z轴的坐标为相机的世界坐标轴的负值)
视口坐标:屏幕坐标系单位化(z轴的坐标为相机的世界坐标轴的负值)
GUI坐标:二维坐标(左上角为(0,0),右下角为(Screen.Width,Screen.height))
各种坐标的转换
世界和局部坐标的转换
世界坐标转换为局部坐标:如计算物体B相对于物体A的局部坐标,A.transform.InverseTransformPoint(B)
局部坐标转换为世界坐标:只能转换子物体的位置到世界坐标,transform.TransformPoint(B.localPosition)
屏幕坐标和世界转换
获得鼠标在屏幕坐标系中的坐标:Input.mousePosition
屏幕坐标转换到世界坐标:Camera.ScreenToWorldPoint(Vector3 position)
世界坐标转换到屏幕坐标:Camera.WorldToScreenPoint(Vector3 position)
屏幕坐标和视口坐标
屏幕坐标转换成视口坐标: Camera.ScreenToViewportPoint(Vector3 position)
视口坐标转换成屏幕坐标:Camera.ViewportToScreenPoint(Vector3 position)
世界坐标和视口坐标
世界坐标转换成视口坐标:Camera.WorldToViewportPoint(Vector3 position)
视口坐标转换成世界坐标:Camera.ViewportToWorldPoint(Vector3 position)
UI坐标和屏幕坐标
屏幕坐标转换成UI局部坐标:
RectTransformUtility.ScreenPointToLocalPointInRectangle(RectTransform rect,Vector2 screenPos,Camera uiCamera,out Vector2 uiLocalPos)
UI世界坐标转换成屏幕坐标:
RectTransformUtility.WorldToScreenPoint(Camera uiCamera,Vector3 worldPos)