├── Pooler.unitypackage ├── README.md └── ObjectPooler.cs /Pooler.unitypackage: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Rfrixy/Generic-Unity-Object-Pooler/HEAD/Pooler.unitypackage -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Generic Unity Object Pooler 2 | An easy to use object-pooler which is efficient and quick to set-up. 3 | 4 | # What is pooling? 5 | It is computationally expensive to instantiate and destroy objects like bullets that get re-used a lot. 6 | Its a lot more effective to instantiate them all in the beginning and to keep re-using them by setting them active/false 7 | 8 | This script can act as a pooling control hub, it will create all pooled objects you need at the start and objects can be called as and when needed by other scripts. 9 | 10 | # How to use? 11 | 1) Create a new empty GameObject. 12 | 2) Create a new script called ObjectPooler 13 | 3) Replace the contents of ObjectPooler with contents of the ObjectPooler script that can be found in this repo. 14 | 4) Attach the script to the GameObject you created. 15 | 5) In the inspector, in the script component, enter the number of gameObjects you want pooled and then add their prefabs to the list. 16 | 6) Increase Amount to Pool to at least 1. If you are unsure how many objects of this type you will need, check the 'should expand' box. 17 | 7) Get the gameObject by using 18 | ``` 19 | GameObject GO = ObjectPooler.SharedInstance.GetPooledObject(0); 20 | // (Instead of instantiating a new one.) 21 | ``` 22 | 23 | 24 | 8) Make sure that the gameObject you are re-using does infact get disabled naturally after a while. 25 | (Otherwise there is no point of pooling) 26 | 9) When you just get the object from the pooler, it will be disabled, remember to set it to active. 27 | 28 | If you still have doubts, you can try running the sample scene included in the package. 29 | Simply download the pooler package then choose to import a custom package from unity, select the downloaded package and navigate to the scene called BallThrow where you can press the spacebar to get lots of object-pooled balls that disable after 5 seconds. 30 | 31 | # Public functions 32 | 33 | Assuming you stored a reference to the objectpooler in a variable called OP. 34 | ``` 35 | ObjectPooler OP; 36 | void Awake(){ 37 | OP = ObjectPooler.SharedInstance; 38 | } 39 | ``` 40 | 1) Getting an object: 41 | ``` 42 | GameObject GO = OP.GetPooledObject(0); // 0 is the index of the object you want 43 | ``` 44 | 2) Getting all gameobjects of a type: 45 | ``` 46 | List objects = OP.GetAllPooledObjects(0); // 0 is the index of the object you want 47 | ``` 48 | 3) Adding a new object during gameplay: 49 | ``` 50 | int indexOfThisObj = OP.AddObject(gameObj, amt, true); 51 | 52 | // where gameObj is the gameobject you want to pool 53 | // amt is an int specifying how many copies you want in the pool 54 | // third argument is a bool which specifies whether this object's pool can expand 55 | ``` 56 | This gameObject can be accessed by using: 57 | ``` 58 | GameObject GO = OP.GetPooledObject(indexOfThisObj); 59 | ``` 60 | -------------------------------------------------------------------------------- /ObjectPooler.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | [System.Serializable] 6 | public class ObjectPoolItem 7 | { 8 | 9 | public GameObject objectToPool; 10 | public int amountToPool; 11 | public bool shouldExpand = true; 12 | 13 | public ObjectPoolItem(GameObject obj, int amt, bool exp = true) 14 | { 15 | objectToPool = obj; 16 | amountToPool = Mathf.Max(amt,2); 17 | shouldExpand = exp; 18 | } 19 | } 20 | 21 | public class ObjectPooler : MonoBehaviour 22 | { 23 | public static ObjectPooler SharedInstance; 24 | public List itemsToPool; 25 | 26 | 27 | public List> pooledObjectsList; 28 | public List pooledObjects; 29 | private List positions; 30 | 31 | void Awake() 32 | { 33 | 34 | SharedInstance = this; 35 | 36 | pooledObjectsList = new List>(); 37 | pooledObjects = new List(); 38 | positions = new List(); 39 | 40 | 41 | for (int i = 0; i < itemsToPool.Count; i++) 42 | { 43 | ObjectPoolItemToPooledObject(i); 44 | } 45 | 46 | } 47 | 48 | 49 | public GameObject GetPooledObject(int index) 50 | { 51 | 52 | int curSize = pooledObjectsList[index].Count; 53 | for (int i = positions[index] + 1; i < positions[index] + pooledObjectsList[index].Count; i++) 54 | { 55 | 56 | if (!pooledObjectsList[index][i % curSize].activeInHierarchy) 57 | { 58 | positions[index] = i % curSize; 59 | return pooledObjectsList[index][i % curSize]; 60 | } 61 | } 62 | 63 | if (itemsToPool[index].shouldExpand) 64 | { 65 | 66 | GameObject obj = (GameObject)Instantiate(itemsToPool[index].objectToPool); 67 | obj.SetActive(false); 68 | obj.transform.parent = this.transform; 69 | pooledObjectsList[index].Add(obj); 70 | return obj; 71 | 72 | } 73 | return null; 74 | } 75 | 76 | public List GetAllPooledObjects(int index) 77 | { 78 | return pooledObjectsList[index]; 79 | } 80 | 81 | 82 | public int AddObject(GameObject GO, int amt = 3, bool exp = true) 83 | { 84 | ObjectPoolItem item = new ObjectPoolItem(GO, amt, exp); 85 | int currLen = itemsToPool.Count; 86 | itemsToPool.Add(item); 87 | ObjectPoolItemToPooledObject(currLen); 88 | return currLen; 89 | } 90 | 91 | 92 | void ObjectPoolItemToPooledObject(int index) 93 | { 94 | ObjectPoolItem item = itemsToPool[index]; 95 | 96 | pooledObjects = new List(); 97 | for (int i = 0; i < item.amountToPool; i++) 98 | { 99 | GameObject obj = (GameObject)Instantiate(item.objectToPool); 100 | obj.SetActive(false); 101 | obj.transform.parent = this.transform; 102 | pooledObjects.Add(obj); 103 | } 104 | pooledObjectsList.Add(pooledObjects); 105 | positions.Add(0); 106 | 107 | } 108 | } 109 | --------------------------------------------------------------------------------