世界坐标转为UI坐标
直接调用WorldToAnchorPos,传入对应的参数返回UGUI坐标
publicstaticVector2WorldToAnchorPos(Vector3 worldPos,Camera mainCamera =null,Vector2? canvasSize =null){if(mainCamera ==null)
mainCamera = Camera.main;Vector2 screenPos = mainCamera.WorldToScreenPoint(worldPos);returnScreenToAnchorPos(screenPos);}publicstaticVector2ScreenToAnchorPos(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://blog.csdn.net/weixin_46292030/article/details/136274298
版权归原作者 weixin_46292030 所有, 如有侵权,请联系我们删除。
版权归原作者 weixin_46292030 所有, 如有侵权,请联系我们删除。