GameObject button;
void SwitchButton()
{
int xPos = -300;
button.transform.position = new Vector3(xPos, button.transform.position.y, 0);
}
void Start()
{
button = GameObejct.Find("button");
SwitchButton();
}
처음에는 위와 같은 구조로 UI의 위치를 변경하려고 했는데, 이상하게 버튼이 x: -300 지점이 아닌 다른 위치로 변경되었다.
그래서 찾아보던 도중, UI는 캔버스 상에서 그려지는 것인데다가 앵커를 기준으로 좌표를 잡는다는 것을..깨달았다.
심지어 캔버스 상에 있는 오브젝트들은 transform이 아니라 rect transform을 쓰는 것을 확인할 수 있었다.
따라서 다음과 같이 코드를 수정해 해결했다.
RectTransform buttonPos;
void SwitchButton()
{
int xPos = -300;
buttonPos.anchoredPosition = new Vector2(xPos, buttonPos.anchoredPosition.y);
}
void Start()
{
buttonPos = GameObejct.Find("button").GetComponent<RectTransform>();
SwitchButton();
}
참고 링크
https://funfunhanblog.tistory.com/219
Unity3D) UGUI UI 위치지정 (RectTransform,anchoredPosition)
UI오브젝트의 위치를 지정하는 것은 월드맵의 존재하는 오브젝트들의 위치를 변경하는 것과 다르다. 월드맵의 존재하는 오브젝트 Transform의 Position의 값으로 위치를 지정해준다. 하지만 UI는 아
funfunhanblog.tistory.com
'Unity' 카테고리의 다른 글
| [Unity/C#] 버튼 여러 번 클릭시 해당 animation 두 번 실행 버그 (0) | 2023.06.16 |
|---|