본문 바로가기
Unity3D/Unity & C#

Unity & C# ~ Anchor에 따른 Position 값 설정

by 캬캬백곰 2022. 6. 7.
728x90

앵커가 가운데일 때
앵커가 좌상단 일 때

앵커 위치가 바뀐 상태에서 position을 세팅할 때, local 또는 world 좌표로 세팅을 했는데 원하는 위치에 가지 않을 때가 있음.

이건 앵커 좌표와 local, world 좌표가 다른 것 때문에 발생함.

이럴때는 코드로 position값을 설정할 때, local이나 world좌표가 아닌 anchored 좌표로 설정하면 해결됨.

anchor 좌표를 설정하기 위해서는 RectTransform 컴포넌트를 찾아야함.

 //anchoredRect의 x좌표를 anchoredObj의 anchoredPosition x 좌표로 설정하기
 var AnchoredX = anchoredObj.GetComponent<RectTransform>().anchoredPosition.x;
 anchoredRect.anchoredPosition = new Vector2(AnchoredX, 0);
728x90
반응형