——世界坐标(World Space)
世界坐标很好理解,它是一个3D坐标。就是游戏物体在你创造世界中的坐标。transfrom.position获得的是物体相对于世界坐标的位置,transfrom.localPosition获得的是物体相对于父物体坐标的位置
模型Mesh保存的顶点坐标均为局部坐标系下的坐标
——屏幕坐标(Screen Space)
屏幕坐标是以像素来定义的,与分辨率有关,例如分辨率为1280*720的屏幕则Screen.width为1280,Screen.height为720。
Screen.width = Camera.main.pixelWidth和Screen.height = Camera.main.pixelHeight只有在相机视口坐标是整个屏幕时才满足。所以不难看出Screen.width是屏幕的分辨率即屏幕的宽度, 而Camera.main.pixelWidth是相机的视口像素大小,也可以理解为相机视口的像素宽度。
屏幕的左下角坐标为(0 , 0),右上角为(Screen.width , Screen.height),z轴坐标是相机的世界坐标中z轴的负值
我们常用的Input.mousePosition和移动端的Input.GetTouch(0).position都是获得的光标在屏幕坐标的位置
——视口坐标(Viewport Space)
视口坐标系其实就是将屏幕坐标系单位化
视口坐标的左下角为(0 , 0),右上角为(1 , 1),z轴坐标是相机的世界坐标中z轴的负值
可用于制作分屏游戏
——GUI坐标
只作了解就好
它的原点在左上角为(0,0),右下角坐标为(Screen.width, Screen.height), 因为这是一个二维的坐标系,所以坐标z
的值永远都为0
——InverseTransformPoint和TransformPoin
例如物体A的世界坐标坐标为(1,2,3),物体B的世界坐标为(2,2,2),现在需要计算物体B相对于物体A的局部坐标,则应该使用A.transform.InverseTransformPoint(B)
——屏幕坐标转世界坐标
登录后复制
Vector3 mousePos = Input.mousePosition;
Vector3 screenToWorld = Camera.main.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, -Camera.main.transform.position.z));
Debug.Log(screenToWorld);
——世界坐标转屏幕坐标
登录后复制
Vector3 worldToScreen = Camera.main.WorldToScreenPoint(transform.position);
Debug.Log(worldToScreen);
——屏幕坐标转视口坐标
登录后复制
Vector3 mousePos = Input.mousePosition;
Vector3 screenToViewport = Camera.main.ScreenToViewportPoint(mousePos);
Debug.Log(screenToViewport);
——视口坐标转屏幕坐标
登录后复制
Vector3 viewportToScreen = Camera.main.ViewportToScreenPoint(new Vector3(1, 1, 0));
Debug.Log(viewportToScreen);
——世界坐标转视口坐标
登录后复制
Vector3 worldToViewport = Camera.main.WorldToViewportPoint(transform.position);
Debug.Log(worldToViewport);
——视口坐标转世界坐标
登录后复制
Vector3 viewportToWord = Camera.main.ViewportToWorldPoint(new Vector3(1, 1, -Camera.main.transform.position.z));
Debug.Log(viewportToWord);
——屏幕坐标转UI坐标
登录后复制
Vector2 mousePos;
RectTransformUtility.ScreenPointToLocalPointInRectangle(transform.parent.GetComponent<RectTransform>(), Input.mousePosition, null, out mousePos);
Debug.Log(mousePos);
1.Canvas的RenderMode为Overlay时,cam参数应该为NULL。
2.rect必须为直接父物体
任何一个坐标转世界坐标时,z的参数都应该为相机在世界坐标中z的负值 从世界坐标转到任何一个坐标时,计算出的z值都是相机的世界坐标中z轴的负值
任何一个坐标转世界坐标时的z值,可以理解为离相机的距离。其实是把屏幕坐标投影到相机的视锥体平面上,如果z的值为0,则相当于投影到了相机的近平面上,但是近平面的最小值为0.01,所以在进行屏幕坐标转世界坐标时,屏幕坐标的z值不能为0(注意Input.mousePosition的z值为0)
而且随着z的值越来越大,投影到的平面也会越来越大,x和y的值也会越来越大
登录后复制
using UnityEngine;
/// <summary>
/// 坐标转换工具
/// </summary>
public static class CCUtils
{
/// <summary>
/// 世界坐标转屏幕坐标
/// </summary>
public static Vector2 World2Screen(Vector3 v, Camera camera = null)
{
if (camera == null)
{
camera = Camera.main;
}
Vector3 worldPos = v;
Vector3 pos = camera.WorldToScreenPoint(worldPos);
return pos;
}
/// <summary>
/// 屏幕坐标转世界坐标
/// </summary>
public static Vector3 Screen2World(Vector2 v, Camera camera = null)
{
if (camera == null)
{
camera = Camera.main;
}
Vector3 screenPos = new Vector3(v.x, v.y, -camera.transform.position.z);
Vector3 pos = camera.ScreenToWorldPoint(screenPos);
return pos;
}
/// <summary>
/// 世界坐标转视口坐标
/// </summary>
public static Vector2 World2Viewport(Vector3 v, Camera camera = null)
{
if (camera == null)
{
camera = Camera.main;
}
Vector3 worldPos = v;
Vector3 pos = camera.WorldToViewportPoint(worldPos);
return pos;
}
/// <summary>
/// 视口坐标转世界坐标
/// </summary>
public static Vector3 Viewport2World(Vector2 v, Camera camera = null)
{
if (camera == null)
{
camera = Camera.main;
}
Vector3 viewportPos = new Vector3(v.x, v.y, -camera.transform.position.z);
Vector3 pos = camera.ViewportToWorldPoint(viewportPos);
return pos;
}
/// <summary>
/// 屏幕坐标转视口坐标
/// </summary>
public static Vector2 Screen2Viewport(Vector2 v, Camera camera = null)
{
if (camera == null)
{
camera = Camera.main;
}
Vector3 screenPos = v;
Vector3 pos = camera.ScreenToViewportPoint(screenPos);
return pos;
}
/// <summary>
/// 视口坐标转屏幕坐标
/// </summary>
public static Vector2 Viewport2Screen(Vector2 v, Camera camera = null)
{
if (camera == null)
{
camera = Camera.main;
}
Vector3 viewportPos = v;
Vector3 pos = camera.ViewportToScreenPoint(viewportPos);
return pos;
}
/// <summary>
/// 屏幕坐标转UI坐标
/// </summary>
public static Vector2 Screen2UI(Vector2 v, RectTransform rect, Camera camera = null)
{
Vector2 pos;
RectTransformUtility.ScreenPointToLocalPointInRectangle(rect, v, camera, out pos);
return pos;
}
/// <summary>
/// 世界坐标转UI坐标
/// </summary>
public static Vector2 World2UI(Vector3 v, RectTransform rect, Camera uiCamera, Camera worldCamera = null)
{
if (worldCamera == null)
{
worldCamera = Camera.main;
}
Vector2 screenPos = World2Screen(v, worldCamera);
Vector2 pos = Screen2UI(screenPos, rect, uiCamera);
return pos;
}
}
免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删