1. GameObject WorldObject;
  2. //this is the ui element
  3. RectTransform UI_Element;
  4. //first you need the RectTransform component of your canvas
  5. RectTransform CanvasRect=Canvas.GetComponent<RectTransform>();
  6. //then you calculate the position of the UI element
  7. //0,0 for the canvas is at the center of the screen, whereas WorldToViewPortPoint treats the lower left corner as 0,0. Because of this, you need to subtract the height / width of the canvas * 0.5 to get the correct position.
  8. Vector2 ViewportPosition=Camara.main.WorldToViewportPoint(WorldObject.transform.position);
  9. Vector2 WorldObject_ScreenPosition=new Vector2(
  10. ((ViewportPosition.x*CanvasRect.sizeDelta.x)-(CanvasRect.sizeDelta.x*0.5f)),
  11. ((ViewportPosition.y*CanvasRect.sizeDelta.y)-(CanvasRect.sizeDelta.y*0.5f)));
  12. //now you can set the position of the ui element
  13. UI_Element.anchoredPosition=WorldObject_ScreenPosition;



- 출처 : https://answers.unity.com/questions/799616/unity-46-beta-19-how-to-convert-from-world-space-t.html

+ Recent posts