본문 바로가기
유니티/코드

GetComponentsInChildren 구현

by fore4022 2024. 10. 12.

GetComponentsInChildren

 

GetComponentsInChildren

유니티에서 제공하는 기본 함수이다. 위의 함수의 기능은, 자신과 자신의 객체들에서 특정 Component를 찾아서 배열로 반환해준다."왜, 이 함수 하나만을 따로 정리하는가?"우선 개발은 언제든지

fore4022.tistory.com


 GetComponentsInChildren과 사용하려 하였으나, 혹시 모른다는 생각으로 직접 구현해 보았다.

 기존의 GetComponentsInChildren과는 다르게, 자기 자신은 탐색 대상에 포함시키지 않았다.


public static List<T> GetComponentsInChildren<T>(Transform transform) where T : Component
    {
        List<T> componentList = new();

        for(int index = 0; index < transform.childCount; index++)
        {
            if(transform.GetChild(index).TryGetComponent<T>(out T component))
            {
                componentList.Add(component);
            }
        }

        return componentList;
    }

 아직 많이 부족한 코드이니, 틀린 점이나 개선 여지가 있는 것을 지적해주세요.

 오늘도 방문해 주셔서 감사합니다.

 

 최근 수정 날짜 : 2024.10.12

'유니티 > 코드' 카테고리의 다른 글

Tweening 구현(1)  (0) 2025.04.29
ScriptableObject 배열 길이 제한하기  (0) 2025.03.18
Non-MonoBehaviour Class에서 Coroutine 실행  (0) 2024.12.31
어드레서블 에셋 씬 로드 구현  (0) 2024.09.11
Input Action 관리  (0) 2024.08.27