世界坐標轉為UI坐標
直接調用WorldToAnchorPos,傳入對應的參數返回UGUI坐標文章來源地址http://www.zghlxwxcb.cn/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;
}
文章來源:http://www.zghlxwxcb.cn/news/detail-850055.html
到了這里,關于Unity坐標系的轉換—世界坐標轉為UI坐標的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!