using System.Collections.Generic; using System.Linq; using UnityEngine; public static class Utility_Component { public static List Get_Childrens_Component(this GameObject parent, bool includeInactive = false, bool destroy = false) where T : Component { List TList = parent.GetComponentsInChildren(includeInactive).ToList(); List 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 Get_Childrens_Component(this Transform parent, bool destroy = false, bool includeInactive = false) where T : Component { return Get_Childrens_Component(parent.gameObject, destroy, includeInactive); } public static bool Try_Get_Component(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(); return true; } component = null; return false; } //public static bool Try_Get_Component(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(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; //} }