世界坐标转为UI坐标
直接调用WorldToAnchorPos,传入对应的参数返回UGUI坐标文章来源地址https://www.toymoban.com/news/detail-850055.html
public static Vector2 WorldToAnchorPos( Vector3 worldPos, Camera mainCamera = null, Vector2? canvasSize = null)
{
if (mainCamera == null)
mainCamera = Camera.main;
Vector2 screenPos = mainCamera.WorldToScreenPoint(worldPos);
return ScreenToAnchorPos(screenPos);
}
public static Vector2 ScreenToAnchorPos(Vector3 screenPos, Vector2? canvasSize = null)
{
Vector2 screenPos2;
screenPos2.x = screenPos.x - (Screen.width / 2f);
screenPos2.y = screenPos.y - (Screen.height / 2f);
Vector2 anchorPos;
if (canvasSize==null)
{
anchorPos.x = (screenPos2.x / Screen.width) * CanvasSize.x;
anchorPos.y = (screenPos2.y / Screen.height) * CanvasSize.y;
}
else
{
anchorPos.x = (screenPos2.x / Screen.width) * canvasSize.Value.x;
anchorPos.y = (screenPos2.y / Screen.height) * canvasSize.Value.y;
}
return anchorPos;
}
文章来源:https://www.toymoban.com/news/detail-850055.html
到了这里,关于Unity坐标系的转换—世界坐标转为UI坐标的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!