Unity object 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);
}
}
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;
}