[Unity]GetComponentsInChildren

jh Seo·2022년 8월 23일
0

유니티

목록 보기
5/42
  • 기본 함수 선언은 이런 식으로 하고
public Component[] GetComponentsInChildren(Type t, bool includeInactive);

DFS방법을 이용해 부모와 자식오브젝트들의 Type 컴퍼넌트들을 찾아 반환한다.
includeInactive는 inactive인 자식 오브젝트도 반환할건지 여부를 설정해주는 것이고,
디폴트는 false로 만약 inactive인 자식오브젝트 반환하려면 true를 설정해줘야한다.

  • 제너릭 버전
public T[] GetComponentsInChildren(bool includeInactive);
public T[] GetComponentsInChildren();

예시는

 //자식오브젝트들을 가진 부모오브젝트
public GameObject buttonsGroup;
//게임오브젝트 배열인 buttons에 buttonsGroup의 자식 오브젝트들을 전부 넣어준다.
GameObject[] buttons= buttonsGroup.GetComponentsInChildren<GameObject>();

이런식으로 사용 가능하다.

  • 부모 오브젝트에 반환하려는 컴포넌트가 있다면 해당 컴포넌트도 반환한다.
    따라서 부모의 컴퍼넌트는 반환하지 않으려면 부모 오브젝트의 이름과 비교한다던지 해서
    빼야한다.
profile
코딩 창고!

0개의 댓글