Unity查找子对象助手

此方法可以在指定父对象一直向内查找,返回查找的子对象,适合层级较多的情况下。

        /// <summary>
        /// 查找对象助手 在指定游戏对象,一直向内查找子对象 返回游戏物体
        /// </summary>
        /// <param name="_findParent">所要查找的父对象</param>
        /// <param name="_targetName">所要查找的对象名称</param>
        /// <returns></returns>
        public static Transform FindChildHelper(Transform _findParent, string _targetName)
        {
    
    
            Transform obj = _findParent.Find(_targetName);
            if (obj != null) return obj;
            Transform target = null;
            for (int i = 0; i < _findParent.childCount; i++)
            {
    
    
                obj = _findParent.GetChild(i);
                target = FindChildHelper(obj, _targetName);
                if(target!=null)
                {
    
    
                    return target;
                }
            }
            return null;
        }

猜你喜欢

转载自blog.csdn.net/weixin_38484443/article/details/107366882
今日推荐