Unity学习笔记003.递归查找子物体/获取子物体组件

public static Transform FindChild(Transform parent,string name)
{
	Transform child = null;
    child = parent.Find(name);
    if (child != null)
    	return child;
    Transform grandchild = null;
    for(int i = 0; i < parent.childCount; i++)
    {
		grandchild = FindChild(parent.GetChild(i), name);
		if (grandchild != null)
	 	return grandchild;
	}
	return null;
}

public static T FindChild<T>(Transform parent, string name) where T : Component
{
	Transform child = null;
	child = FindChild(parent, name);
	if (child != null)
		 return child.GetComponent<T>();
	return null;
}

你可能感兴趣的:(UnityProject)