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

 

删除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();
    }
}

相关文章:

  • 2021-07-10
  • 2021-07-21
  • 2021-12-27
  • 2021-05-19
  • 2021-09-01
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-07-04
  • 2021-12-13
  • 2021-10-28
  • 2022-12-23
  • 2022-12-23
  • 2022-01-05
相关资源
相似解决方案