63 lines
2.1 KiB
C#
63 lines
2.1 KiB
C#
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
public static class Utility_Component
|
|
{
|
|
public static List<T> Get_Childrens_Component<T>(this GameObject parent, bool includeInactive = false, bool destroy = false) where T : Component
|
|
{
|
|
List<T> TList = parent.GetComponentsInChildren<T>(includeInactive).ToList();
|
|
List<T> TListReal = new();
|
|
for (int i = 0; i < TList.Count; i++)
|
|
{
|
|
if (TList[i].transform.parent == parent.transform)
|
|
{
|
|
TListReal.Add(TList[i]);
|
|
}
|
|
}
|
|
if (destroy)
|
|
{
|
|
for (int i = 0; i < TListReal.Count; i++)
|
|
{
|
|
if (TListReal[i] == parent)
|
|
{
|
|
continue;
|
|
}
|
|
GameObject.DestroyImmediate(TListReal[i].gameObject);
|
|
}
|
|
return null;
|
|
}
|
|
return TListReal;
|
|
}
|
|
public static List<T> Get_Childrens_Component<T>(this Transform parent, bool destroy = false, bool includeInactive = false) where T : Component
|
|
{
|
|
return Get_Childrens_Component<T>(parent.gameObject, destroy, includeInactive);
|
|
}
|
|
public static bool Try_Get_Component<T>(this GameObject gameObject, out T component, bool add = true) where T : Component
|
|
{
|
|
if (gameObject.TryGetComponent(out T _component))
|
|
{
|
|
component = _component;
|
|
return true;
|
|
}
|
|
if (add)
|
|
{
|
|
component = gameObject.AddComponent<T>();
|
|
return true;
|
|
}
|
|
component = null;
|
|
return false;
|
|
}
|
|
//public static bool Try_Get_Component<T>(this Component gameObject, out T component, bool add = true) where T : Component
|
|
//{
|
|
// var b = Try_Get_Component(gameObject, out T _component, add);
|
|
// component = _component;
|
|
// return b;
|
|
//}
|
|
//public static bool Try_Get_Component<T>(this MonoBehaviour gameObject, out T component, bool add = true) where T : Component
|
|
//{
|
|
// var b = Try_Get_Component(gameObject, out T _component, add);
|
|
// component = _component;
|
|
// return b;
|
|
//}
|
|
} |