废话不多说,直接上代码。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
public List CH = new List();//储存物体的列表
public Material Dissolve;
// Start is called before the first frame update
void Start()
{
FindChild(this.gameObject);//找到节点下的所有子物体
DoDissolve();
}
void FindChild(GameObject child)
{
//利用for循环 获取物体下的全部子物体
for (int c = 0; c < child.transform.childCount; c++)
{
//如果子物体下还有子物体 就将子物体传入进行回调查找 直到物体没有子物体为止
if (child.transform.GetChild(c).childCount > 0)
{
FindChild(child.transform.GetChild(c).gameObject);
}
CH.Add(child.transform.GetChild(c).gameObject);
}
}
}
获取全部子物体后,可通过list列表对物体进行批处理;
如添加或移除其子物体的组件,判断其子物体是否有某个组件等等。



