using System;
using UnityEngine;

public static class MyTools
{
    public static void SetChildrenActive(this GameObject theGameObject, bool theActive)
    {
        foreach (Transform ChildTransform in theGameObject.transform)
        {
            ChildTransform.gameObject.SetActive(theActive);
        }
    }

    public static void SetChildren<T>(this Transform theGameObject, Action<T> action)
    {
        foreach (Transform child in theGameObject)
        {
            T obj = child.GetComponent<T>();
            action?.Invoke(obj);
        }
    }

    /// <summary>
    /// 递归查找子物体
    /// </summary>
    /// <param name="tr"></param>
    /// <param name="childName"></param>
    /// <returns></returns>
    public static Transform FindChildren(Transform tr, string childName)
    {
        for (int i = 0; i < tr.childCount; i++)
        {
            if (tr.GetChild(i).name == childName)
            {
                Transform t = tr.GetChild(i);
                if (t != null)
                {
                    return t;
                }
            }
            else
            {
                Transform t = FindChildren(tr.GetChild(i), childName);
                if (t != null)
                {
                    return t;
                }
            }
        }
        return null;
    }
}