728x90
public static T FindChild<T>(GameObject oj, string name = null) where T : UnityEngine.Object
{
if (oj == null)
return null;
//첫번째 방법
for (int i = 0; i < oj.transform.childCount; i++)
{
Transform transform = oj.transform.GetChild(i);
if (transform.name == name)
{
T component = transform.GetComponent<T>();
if (component != null)
return component;
}
}
//두번째 방법
foreach(T component in oj.GetComponentsInChildren<T>())
{
if (component.name == name)
return component;
}
return null;
}
728x90
'unity > 유니티 초보' 카테고리의 다른 글
Unity3D - Custom Editor 제작 (0) | 2024.01.12 |
---|---|
Unity 광고 추가 방법 - UnityAds 최신버전 (0) | 2023.12.26 |
Unity : 카메라 캐릭터 따라다니기 - 중간에 방해물이 있는경우 (0) | 2023.01.05 |
Unity : 카메라 캐릭터 따라다니기 (0) | 2023.01.05 |
Unity: raycasting - Object확인 방법 (0) | 2023.01.05 |