// Returns an instance of the prefab public GameObject GetObject() { GameObject spawnedGameObject; // if there is an inactive instance of the prefab ready to return, return that if (inactiveInstances.Count > 0) { // remove the instance from teh collection of inactive instances spawnedGameObject = inactiveInstances.Pop(); } // otherwise, create a new instance else { spawnedGameObject = (GameObject)GameObject.Instantiate(prefab); // add the Pooled_Object component to the prefab so we know it came from this pool Pooled_Object pooledObject = spawnedGameObject.AddComponent <Pooled_Object>(); pooledObject.pool = this; } // put the instance in the root of the scene and enable it spawnedGameObject.transform.SetParent(null); spawnedGameObject.SetActive(true); // return a reference to the instance return(spawnedGameObject); }
// This ensures objects that we are caching don't get polluted with our tag script, so destroy on exit void OnApplicationQuit() { foreach (DictionaryEntry entry in _objects) { Pooled_Object po = (Pooled_Object)entry.Value; Component.DestroyImmediate(po.main_obj.GetComponent <PoolID>(), true); } }
private IEnumerator Destroy_Object(PoolID obj_pid, float time = 0.0f) { yield return(new WaitForSeconds(time)); Pooled_Object po = (Pooled_Object)_objects[obj_pid.id]; po.ReturnObject(obj_pid.gameObject); }
/// <summary> /// Pretend to instantiate the specified object, put it in a pool. If the object is not in the existing cache pool /// put it in and reuse. If the object already exist, reuse the exisiting pool objects. If there is no more objects /// in the pool, expanded the pool to add more. /// </summary> /// <param name="obj">Object.</param> public GameObject PS_Instantiate(GameObject obj) { GameObject o = null; PoolID obj_pid = obj.GetComponent <PoolID>(); if (obj_pid != null && _objects.Count == 0) { Debug.LogError("PoolID is already contained on the prefabs. Make sure to remove them before start."); return(null); } // Cahced already, search for the correct pool if (obj_pid != null) { Pooled_Object po = (Pooled_Object)_objects[obj_pid.id]; if (po.main_obj.GetComponent <PoolID>().id == obj_pid.id) { o = po.GetObject(); } } // No cache of the object else { // Create new object and have it generate a pool id Pooled_Object po = new Pooled_Object(obj); // Get pool id for this object now obj_pid = obj.GetComponent <PoolID>(); // Add object into our table of pooled objects _objects.Add(obj_pid.id, po); // Get the object of the current pooled object o = po.GetObject(); if (object_hierarchy) { // Root the pooled objects into a true root object for the entire pooling system po.pool_root.transform.parent = _root.transform; } } return(o); }
// Return an instance of the prefab to the pool public void ReturnObject(GameObject toReturn) { Pooled_Object pooledObject = toReturn.GetComponent <Pooled_Object>(); // if the instance came from this pool, return it to the pool if (pooledObject != null && pooledObject.pool == this) { // make the instance a child of this and disable it toReturn.transform.SetParent(transform); toReturn.SetActive(false); // add the instance to the collection of inactive instances inactiveInstances.Push(toReturn); } // otherwise, just destroy it else { Debug.LogWarning(toReturn.name + " was returned to a pool it wasn't spawned from! Destroying."); Destroy(toReturn); } }