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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/baidu_33643757/article/details/84247847
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;
}

猜你喜欢

转载自blog.csdn.net/baidu_33643757/article/details/84247847