[Unity/C#] UI 위치가 원하는 대로 변경되지 않던 문제

2023. 6. 18. 22:27·Unity
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
'Unity' 카테고리의 다른 글
  • [Unity/C#] 버튼 여러 번 클릭시 해당 animation 두 번 실행 버그
김왈왈이
김왈왈이
  • 김왈왈이
    저장소
    김왈왈이
  • 전체
    오늘
    어제
  • 링크

    • GitHub
    • 분류 전체보기 (40)
      • TIL (1)
      • Spring (5)
      • Andriod (4)
      • Unity (2)
      • Java (1)
      • C++ (0)
      • HTML CSS JavaScript (1)
      • 프로젝트 (11)
      • 코딩테스트 (11)
      • 기타 (4)
  • 최근 글

  • 태그

    그리디 알고리즘
    c#
    Unity
    silver v
    게임개발
    경합상태오류
    유니티
    html
    단축키
    브루트포스 알고리즘
    에러
    백준
    수학
    springboot
    silver iv
    Quaternion.Euler()
    java
    하위오브젝트반환
    프로토타입
    오류
    UnassignedReferenceException
    게임
    공격구현
    구현
    [Serializable]
    C++
    자료구조
    개발
    게임프로토타입
    유니티게임
  • hELLO· Designed By정상우.v4.10.4
김왈왈이
[Unity/C#] UI 위치가 원하는 대로 변경되지 않던 문제
상단으로

티스토리툴바