/// <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> /// Spawns a gameobject. /// </summary> /// <param name="pos">The position to spawn the gameobject.</param> /// <param name="rot">The rotation to spawn the gameobject.</param> /// <returns>The spawned gameobject.</returns> public GameObject Spawn(Vector3 pos, Quaternion rot) { GameObject com = null; if (DespawnedElements.Count != 0) { while (com == null && DespawnedElements.Count > 0) { com = DespawnedElements.Dequeue(); } if (com == null) { com = Instantiate(); } } else { com = Instantiate(); } com.transform.position = pos; com.transform.transform.rotation = rot; ActiveObject.Add(com); com.gameObject.SetActive(true); if (OnObjectSpawn != null) { OnObjectSpawn.Invoke(com, this); } return(com); }
/// <summary> /// Instantiate a new object pool with a prefab and prealocates a desired number(s) of that object. /// </summary> /// <param name="prefab">The target gameobject which should be cloned. Can not be null.</param> /// <param name="preallocateAmount">The amount of object which should be prealocated.</param> public ObjectPool(GameObject prefab, int preallocateAmount) : this(prefab) { for (int i = 0; i < preallocateAmount; i++) { DespawnedElements.Enqueue(Instantiate()); } }
public void Allocate(int amount) { for (int i = 0; i < amount; i++) { GameObject com = (GameObject)GameObject.Instantiate(Prefab); com.gameObject.SetActive(false); com.transform.parent = Parent; DespawnedElements.Enqueue(com); } }
public void DeAllocate(int amount) { for (int i = 0; i < amount; i++) { if (DespawnedElements.Count == 0) { break; } Destroy(DespawnedElements.Dequeue()); } }
public bool Despawn(GameObject pObject, bool DestroyObject = false) { if (ActiveObject.Remove(pObject)) { if (DestroyObject) { Destroy(pObject); } else { pObject.SetActive(false); DespawnedElements.Enqueue(pObject); } return(true); } return(false); }
public GameObject Spawn(Vector3 pos, Quaternion rot) { GameObject com = null; if (DespawnedElements.Count != 0) { com = DespawnedElements.Dequeue(); } else { com = Instantiate(); } com.transform.position = pos; com.transform.transform.rotation = rot; ActiveObject.Add(com); com.gameObject.SetActive(true); return(com); }
/// <summary> /// Checks if a gameobject is in the objectpool. /// </summary> /// <param name="item">The gameobject to check.</param> /// <returns>True if the gameobject is in the objectpool, otherwise false.</returns> public bool Contains(GameObject item) { return(ActiveObject.Contains(item) || DespawnedElements.Contains(item)); }