/// <summary> /// Despawns a object. /// </summary> /// <param name="pObject">The object to despawn.</param> /// <param name="destroyObject">Should the despawned object also get destroyed?</param> /// <param name="nullcheck">Should the list get interated to removed null values?</param> /// <returns>True if the object was removed correctly.</returns> private bool DespawnInternal(GameObject pObject, bool destroyObject, bool nullcheck) { if (nullcheck) { ActiveObject.RemoveAll(g => g == null); } if (ActiveObject.Remove(pObject)) { if (OnObjectDeSpawn != null) { OnObjectDeSpawn.Invoke(pObject, this); } if (destroyObject) { if (pObject.GetComponent <IDespawnEvent>() != null) { OnObjectDeSpawn -= new DespawnEvent(pObject.GetComponent <IDespawnEvent>().OnDespawned); } Destroy(pObject); } else { pObject.SetActive(false); DespawnedElements.Enqueue(pObject); } return(true); } return(false); }
/// <summary> /// Despawns all the active objects and tell rather they should be enqueued for later use or completely destroyed. /// </summary> /// <param name="destroyObject">Should the despawned objects also get destroyed?</param> /// <returns>True if all objects were despawed correctly.</returns> public bool DespawnAllActiveObjects(bool destroyObject) { ActiveObject.RemoveAll(g => g == null); while (ActiveObject.Count != 0) { if (!DespawnInternal(ActiveObject[0], destroyObject, false)) { return(false); } } return(true); }