| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- using UnityEngine;
- using System.Collections.Generic;
- namespace WXB
- {
- public class DelayDestory : MonoBehaviour
- {
- static List<GameObject> Destorys = new List<GameObject>();
- static List<GameObject> DestroyImmediates = new List<GameObject>();
- #if UNITY_EDITOR
- static DelayDestory()
- {
- UnityEditor.EditorApplication.update += UpdateEditor;
- }
- static void UpdateEditor()
- {
- if (!Application.isPlaying)
- {
- OnUpdate();
- }
- }
- #endif
- static DelayDestory instance = null;
- static void Init()
- {
- #if UNITY_EDITOR
- if (!Application.isPlaying)
- return;
- #endif
- if (instance == null)
- {
- instance = (new GameObject("DelayDestory")).AddComponent<DelayDestory>();
- //instance.gameObject.hideFlags = HideFlags.HideAndDontSave;
- }
- }
- public static void Destroy(GameObject go)
- {
- Init();
- Destorys.Add(go);
- }
- public static void DestroyImmediate(GameObject go)
- {
- Init();
- DestroyImmediates.Add(go);
- }
- void LateUpdate()
- {
- #if UNITY_EDITOR
- if (!Application.isPlaying)
- return;
- #endif
- OnUpdate();
- }
- static void OnUpdate()
- {
- for (int i = 0; i < Destorys.Count; ++i)
- {
- if (Destorys[i] != null)
- {
- Object.Destroy(Destorys[i]);
- }
- }
- Destorys.Clear();
- for (int i = 0; i < DestroyImmediates.Count; ++i)
- {
- if (DestroyImmediates[i] != null)
- {
- Object.DestroyImmediate(DestroyImmediates[i]);
- }
- }
- DestroyImmediates.Clear();
- }
- }
- }
|