Unity中将世界坐标转到UGUI坐标
2021-3-29 10:03:21 点击:
public static Vector3 WorldToUI(Camera camera,Vector3 pos){
CanvasScaler scaler = GameObject.Find("UIRoot").GetComponent<CanvasScaler>();
float resolutionX = scaler.referenceResolution.x;
float resolutionY = scaler.referenceResolution.y;
Vector3 viewportPos = camera.WorldToViewportPoint(pos);
Vector3 uiPos = new Vector3(viewportPos.x * resolutionX - resolutionX * 0.5f,
viewportPos.y * resolutionY - resolutionY * 0.5f,0);
return uiPos;
}
public static Vector2 WorldToUIPoint(Transform worldGo)
{
Vector2 pos;
RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas.transform as RectTransform,
gameCamera.WorldToScreenPoint(worldGo.transform.position), canvas.worldCamera, out pos);
RectTransform rect = transform.transform as RectTransform;
return pos;
}
CanvasScaler scaler = GameObject.Find("UIRoot").GetComponent<CanvasScaler>();
float resolutionX = scaler.referenceResolution.x;
float resolutionY = scaler.referenceResolution.y;
Vector3 viewportPos = camera.WorldToViewportPoint(pos);
Vector3 uiPos = new Vector3(viewportPos.x * resolutionX - resolutionX * 0.5f,
viewportPos.y * resolutionY - resolutionY * 0.5f,0);
return uiPos;
}
public static Vector2 WorldToUIPoint(Transform worldGo)
{
Vector2 pos;
RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas.transform as RectTransform,
gameCamera.WorldToScreenPoint(worldGo.transform.position), canvas.worldCamera, out pos);
RectTransform rect = transform.transform as RectTransform;
return pos;
}
- 上一篇:UGUI与NGUI的区别与优缺点介绍 2021/3/31
- 下一篇:HoloLens 2移动、旋转全息影像并调整大小 2021/3/26