GameObject WorldObject;
//this is the ui element
RectTransform UI_Element;
//first you need the RectTransform component of your canvas
RectTransform CanvasRect=Canvas.GetComponent<RectTransform>();
//then you calculate the position of the UI element
//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.
Vector2 ViewportPosition=Camara.main.WorldToViewportPoint(WorldObject.transform.position);
Vector2 WorldObject_ScreenPosition=new Vector2(
((ViewportPosition.x*CanvasRect.sizeDelta.x)-(CanvasRect.sizeDelta.x*0.5f)),
((ViewportPosition.y*CanvasRect.sizeDelta.y)-(CanvasRect.sizeDelta.y*0.5f)));
//now you can set the position of the ui element
UI_Element.anchoredPosition=WorldObject_ScreenPosition;
- 출처 : https://answers.unity.com/questions/799616/unity-46-beta-19-how-to-convert-from-world-space-t.html
'Game development > Unity' 카테고리의 다른 글
유니티 랜덤 값 얻기 관련 좋은 자료 (0) | 2018.04.20 |
---|---|
유니티 스크롤뷰 내의 Cotent들 매우 간단하게 배치하기 (0) | 2018.04.09 |
유니티에서 git 사용시 .obj 모델 메쉬 파일들이 커밋이 안될 때 해결 방법 (0) | 2018.03.22 |
유니티 Raycast LayerMask 사용할 때 주의해야할 점 (0) | 2018.03.19 |
유니티에서 UI Button을 계속 누르고 있는 상태를 Check하는 법 (0) | 2018.03.16 |