//递归查找子物体
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RecursionFind : MonoBehaviour {
public Transform parent;
void Start () {
FindChild(“Child”, parent);
Debug.Log(FindChild(“Child”, parent).name);
}
Transform FindChild(string name, Transform parent)
{
if (parent.name == name)
{
return parent;
}
if (parent.childCount < 1)
{
return null;
}
Transform target = null;
for(int i = 0;i < parent.childCount;i++)
{
Transform t= parent.GetChild(i).transform;
target = FindChild(name, t);
if (target != null)
{
break;
}
}
return target;
}
}