53 lines
1.6 KiB
C#
53 lines
1.6 KiB
C#
using UnityEngine;
|
|
using Object = UnityEngine.Object;
|
|
|
|
public class Util
|
|
{
|
|
// recursive : 자식의 자식의 자식까지 재귀적으로 찾을지 여부
|
|
public static T FindChild<T>(GameObject go, string name = null, bool recursive = false) where T : Object
|
|
{
|
|
if(go == null) return null;
|
|
|
|
// 재귀적으로 찾을것인가?
|
|
if (recursive)
|
|
{
|
|
foreach(T component in go.GetComponentsInChildren<T>())
|
|
{
|
|
if (string.IsNullOrEmpty(name) || component.name == name)
|
|
return component;
|
|
}
|
|
}
|
|
// 재귀적으로 찾지 않을것인가?
|
|
else
|
|
{
|
|
for (int i = 0; i < go.transform.childCount; i++)
|
|
{
|
|
Transform transform = go.transform.GetChild(i);
|
|
if (string.IsNullOrEmpty(name) || transform.name == name)
|
|
{
|
|
T component = transform.GetComponent<T>();
|
|
if (component != null) return component;
|
|
}
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
// GameObject를 찾는 용도
|
|
public static GameObject FindChild(GameObject go, string name = null, bool recursive = false)
|
|
{
|
|
Transform transform = FindChild<Transform>(go, name, recursive);
|
|
if (transform == null) return null;
|
|
|
|
return transform.gameObject;
|
|
}
|
|
|
|
public static T GetOrAddComponent<T>(GameObject go) where T : Component
|
|
{
|
|
T component = go.GetComponent<T>();
|
|
if (component == null) component = go.AddComponent<T>();
|
|
return component;
|
|
}
|
|
}
|