赞
踩
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class DelGameObject : MonoBehaviour {
-
- public string DelName;
-
-
- public List<GameObject> list;
-
-
- public void GetTarget()
- {
- list = new List<GameObject>();
- GetChildObj(transform, DelName, ref list);
- }
-
- public void DelGameObj()
- {
-
- for (int i = 0; i < list.Count; i++)
- {
- // DestroyImmediate(list[i]);
- list[i].SetActive(false);
- }
- list = null;
- }
- public static void GetChildObj(Transform Trans,string cname, ref List<GameObject> list)
- {
- for (int i = 0; i < Trans.childCount; i++)
- {
- GameObject t = Trans.GetChild(i).gameObject;
- if (t.name == cname)
- {
- if (!list.Contains(t))
- list.Add(t);
- }
- GetChildObj(Trans.GetChild(i), cname, ref list);
- }
- }
- }
编辑器扩展:
- using System.Collections;
- using System.Collections.Generic;
- using UnityEditor;
- using UnityEngine;
-
- [CustomEditor(typeof(DelGameObject))]
- public class DelGameObjEditor : Editor
- {
- DelGameObject delGameObject;
- private void OnEnable()
- {
- delGameObject = target as DelGameObject;
- }
-
- public override void OnInspectorGUI()
- {
- base.OnInspectorGUI();
- if (GUILayout.Button("获取"))
- {
- delGameObject.GetTarget();
- }
- if (GUILayout.Button("删除"))
- {
- delGameObject.DelGameObj();
- }
-
-
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。