当前位置:   article > 正文

unity 编辑器模式下一次性删除多个同名子节点或者多个同名脚本_unity 删除子节点

unity 删除子节点

 

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class DelGameObject : MonoBehaviour {
  5. public string DelName;
  6. public List<GameObject> list;
  7. public void GetTarget()
  8. {
  9. list = new List<GameObject>();
  10. GetChildObj(transform, DelName, ref list);
  11. }
  12. public void DelGameObj()
  13. {
  14. for (int i = 0; i < list.Count; i++)
  15. {
  16. // DestroyImmediate(list[i]);
  17. list[i].SetActive(false);
  18. }
  19. list = null;
  20. }
  21. public static void GetChildObj(Transform Trans,string cname, ref List<GameObject> list)
  22. {
  23. for (int i = 0; i < Trans.childCount; i++)
  24. {
  25. GameObject t = Trans.GetChild(i).gameObject;
  26. if (t.name == cname)
  27. {
  28. if (!list.Contains(t))
  29. list.Add(t);
  30. }
  31. GetChildObj(Trans.GetChild(i), cname, ref list);
  32. }
  33. }
  34. }

编辑器扩展:

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEditor;
  4. using UnityEngine;
  5. [CustomEditor(typeof(DelGameObject))]
  6. public class DelGameObjEditor : Editor
  7. {
  8. DelGameObject delGameObject;
  9. private void OnEnable()
  10. {
  11. delGameObject = target as DelGameObject;
  12. }
  13. public override void OnInspectorGUI()
  14. {
  15. base.OnInspectorGUI();
  16. if (GUILayout.Button("获取"))
  17. {
  18. delGameObject.GetTarget();
  19. }
  20. if (GUILayout.Button("删除"))
  21. {
  22. delGameObject.DelGameObj();
  23. }
  24. }
  25. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/332934
推荐阅读
相关标签
  

闽ICP备14008679号