unity pool code example

Example 1: unity object pooling

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


[System.Serializable]
public class ObjectPoolItem
{

	public GameObject objectToPool;
	public int amountToPool;
	public bool shouldExpand = true;

	public ObjectPoolItem(GameObject obj, int amt, bool exp = true)
	{
		objectToPool = obj;
		amountToPool = Mathf.Max(amt,2);
		shouldExpand = exp;
	}
}

public class ObjectPooler : MonoBehaviour
{
	public static ObjectPooler SharedInstance;
	public List<ObjectPoolItem> itemsToPool;


	public List<List<GameObject>> pooledObjectsList;
	public List<GameObject> pooledObjects;
	private List<int> positions;

	void Awake()
	{

		SharedInstance = this;

		pooledObjectsList = new List<List<GameObject>>();
		pooledObjects = new List<GameObject>();
		positions = new List<int>();


		for (int i = 0; i < itemsToPool.Count; i++)
		{
			ObjectPoolItemToPooledObject(i);
		}

	}


	public GameObject GetPooledObject(int index)
	{

		int curSize = pooledObjectsList[index].Count;
		for (int i = positions[index] + 1; i < positions[index] + pooledObjectsList[index].Count; i++)
		{

			if (!pooledObjectsList[index][i % curSize].activeInHierarchy)
			{
				positions[index] = i % curSize;
				return pooledObjectsList[index][i % curSize];
			}
		}

		if (itemsToPool[index].shouldExpand)
		{

			GameObject obj = (GameObject)Instantiate(itemsToPool[index].objectToPool);
			obj.SetActive(false);
			obj.transform.parent = this.transform;
			pooledObjectsList[index].Add(obj);
			return obj;

		}
		return null;
	}

	public List<GameObject> GetAllPooledObjects(int index)
	{
		return pooledObjectsList[index];
	}


	public int AddObject(GameObject GO, int amt = 3, bool exp = true)
	{
		ObjectPoolItem item = new ObjectPoolItem(GO, amt, exp);
		int currLen = itemsToPool.Count;
		itemsToPool.Add(item);
		ObjectPoolItemToPooledObject(currLen);
		return currLen;
	}


	void ObjectPoolItemToPooledObject(int index)
	{
		ObjectPoolItem item = itemsToPool[index];

		pooledObjects = new List<GameObject>();
		for (int i = 0; i < item.amountToPool; i++)
		{
			GameObject obj = (GameObject)Instantiate(item.objectToPool);
			obj.SetActive(false);
			obj.transform.parent = this.transform;
			pooledObjects.Add(obj);
		}
		pooledObjectsList.Add(pooledObjects);
		positions.Add(0);

	}
}

/* <HowToUse>
 *
 How to use?
Create a new empty GameObject.
Create a new script called ObjectPooler
Replace the contents of ObjectPooler with contents of the ObjectPooler script that can be found in this repo.
Attach the script to the GameObject you created.
In the inspector, in the script component, enter the number of gameObjects you want pooled and then add their prefabs to the list.
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.
Get the gameObject by using
GameObject GO = ObjectPooler.SharedInstance.GetPooledObject(0);
 // (Instead of instantiating a new one.)
Make sure that the gameObject you are re-using does infact get disabled naturally after a while. (Otherwise there is no point of pooling)
When you just get the object from the pooler, it will be disabled, remember to set it to active.
If you still have doubts, you can try running the sample scene included in the package. 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.

Public functions
Assuming you stored a reference to the objectpooler in a variable called OP.

ObjectPooler OP;
void Awake(){
  OP = ObjectPooler.SharedInstance;
}
Getting an object:
GameObject GO = OP.GetPooledObject(0); // 0 is the index of the object you want
Getting all gameobjects of a type:
List<GameObject> objects = OP.GetAllPooledObjects(0); // 0 is the index of the object you want
Adding a new object during gameplay:
int indexOfThisObj = OP.AddObject(gameObj, amt, true);

// where gameObj is the gameobject you want to pool
// amt is an int specifying how many copies you want in the pool
// third argument is a bool which specifies whether this object's pool can expand
This gameObject can be accessed by using:

GameObject GO = OP.GetPooledObject(indexOfThisObj);
 *
 * </HowToUse>
 */

Example 2: unity object pooling

List<GameObject>list;

public PoolSystem(int size, GameObject prefab){  
  list = new List<GameObject>();     
  for(int i = 0 ; i < size; i++){       
    GameObject obj = (GameObject)Instantiate(prefab);  
    list.Add(obj);     
  } 
}

 public GameObject GetObject(){
     if(list.Count > 0){ 
        GameObject obj = list[0];
        obj.RemoveAt(0);
        return obj;
     }
     return null;
 }

public void DestroyObjectPool(GameObject obj){
      list.Add(obj);
      obj.SetActive(false);
 }

public void ClearPool(){
     for(int i = list.Count - 1; i > 0; i--){
         GameObject obj = list.RemoveAt(i);
         Destroy(obj);
     }
     list = null;
 }