删除GameObject上除了Quality子物体之外其他子物体的组件(但不删除transform)

 


如题需求

using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Collections.Generic;
public class DestroySubComponent{

    [MenuItem("Assets/Example/DestroySubComponent")]
    public static void DestroySubCom()
    {
        UnityEngine.Object[] selection = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.DeepAssets);
        foreach (Object obj in selection)
        {
            GameObject go = obj as GameObject;
            if (go != null)
            {

      //获取所有子物体
                foreach (Transform t in go.transform)
                {
                    if (!t.name.Equals("Quality"))
                    {
                        UnityEngine.Object.DestroyImmediate(t.renderer, true);
                        MeshFilter mf = t.GetComponent<MeshFilter>();
                        if (mf) { UnityEngine.Object.DestroyImmediate(mf, true); }
                    }
                }
               
            }
        }
        AssetDatabase.SaveAssets();
    }
}

posted @ 2012-07-18 16:44  渡蓝  阅读(267)  评论(0编辑  收藏  举报