[Unity] GetComponentsInChildren 검색 범위

한마디로!

GetComponentsInChildren<T>는 T유형의 컴포넌트를 가진 오브젝트들의 컴포넌트를 저장한 배열이다.

 

 

ex)

Transform[] seatTransform = GetComponentsInChildren<Transform>();

위와 같이 찾으려는 컴포넌트 타입의 배열이다!!

 

이 스크립트가 부착된 오브젝트를 대상으로 사용한다면 아래와 같이 사용할 수 있다.

myResults = GetComponentsInChildren<ComponentType>()

또는, 이 스크립트가 부착되지 않은 오브젝트를 대상으로 사용한다면 아래와 같이 사용할 수 있다.

myResults = otherComponent.GetComponentsInChildren<ComponentType>()

 

 

그러면 이 배열에 저장하기 위해 검색하는 범위가 어떻게 되는가?

이름이 children뭐시기라 찾으려는 오브젝트의 자식 오브젝트에서만 컴포넌트를 찾는줄 알았는데 아니었다.

찾으려는 오브젝트 + 그 오브젝트의 모든 자식 오브젝트에서 검색해서 저장한다.

그리고 자식의 자식 오브젝트도 다 포함한다!


< 참고자료 >

https://docs.unity3d.com/ScriptReference/Component.GetComponentsInChildren.html

 

Unity - Scripting API: Component.GetComponentsInChildren

The typical usage for this method is to call it from a MonoBehaviour script (which itself is a type of component), to find references to other Components or MonoBehaviours attached to the same GameObject as that script, or its child GameObjects. In this ca

docs.unity3d.com

 

'Unity > Client' 카테고리의 다른 글

[Unity] Json  (0) 2023.05.18
[Unity] 좌표계와 함수  (0) 2023.05.07
[Unity] 이미지로 material 만들기  (0) 2023.04.11
[Unity] TextMeshPro 색 변경하기  (0) 2022.02.28
[Unity] Collider의 속성과 메서드  (0) 2022.01.05