Object pooling for performance in Unity


Object pooling is a technique that you're bound to need at some point in your game dev career. In fact, you may even be using some form of it (or your game engine might) without realizing it. In a nutshell, it is equivalent to caching. Rather than instantiating objects at runtime as you need them, you should instantiate everything you will need (or estimate you will need) beforehand, and pull from this "pool" when you need them. Instead of destroying objects when they are "dead," you simply disable them and put them back into the pool.

The classic example in games are bullets: rather than instantiating a new bullet every time characters fire their weapons, and then destroying them when off screen or hitting another character, you simply disable them and move them off screen, then enable and move them back on screen as needed. Obviously, it doesn't just apply to bullets, so keep an eye out for anything in your game that is instantiated frequently. The most common cases are projectiles and effects, such as explosions, dust, debris, etc, however you could even go as far as using it for common shared UI components for the ultimate in snappy performance.

The benefits to object pooling are: fewer game hiccups and lag due to expensive instantiation and destruction, and better estimation of memory needs up front since pooled objects are always loaded. If you have a lot of objects, you will definitely notice and feel these improvements right away. The only downside is from the developer's perspective: it requires more attention to initialization of variables in objects as they are enabled and disabled, rather than created and destroyed. In Unity, this means you should handle this logic in OnEnable() and OnDisable() in game objects. Similarly, rather than creating and destroying, you are enabling and disabling the pooled objects.

There are many object pooling scripts in the Asset Store, but it's easy enough to create your own if you understand the concept. As with all in-house solutions, you can tailor it slightly to better fit your exact needs when crafting your own. I'm including the full script here in case you'd like to use it in your own projects (or improve upon it). Some notes about my implementation:

  • The ObjectPool.cs script is attached to an empty game object in the scene.
  • It's developed as a singleton, so it has a static reference: ObjectPool.shared
  • It's designed to store any number of pooled object types, with each containing a game object, the amount to start in the pool, and whether or not the amount can dynamically grow or not. You set these up simply on the game object (see the screenshot example below).
  • To facilitate storing anything, the main data structure is a Dictionary full of Lists, with the keys of the dictionary being the names of the prefabs / game objectsto be stored.
  • GetPooledObject() returns a regular instantiated prefab if there is no associated pool, so it's safe to call from scripts that don't know if there's a pool or not.

Note: This hasn't really been stress-tested too much, but so far it seems to be working great. I'm not sure if making it too flexible (a Dictionary of Lists) is actually more expensive than instantiating and destroying, thus defeating the purpose, but I imagine it isn't and I'll run some tests later.

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

[System.Serializable]
public class PooledObject
{
	public Object objectToPool;
	public int pooledAmount = 20;
	public bool canGrow = true;
}

public class ObjectPool : MonoBehaviour
{
	public static ObjectPool shared;
	public PooledObject[] pooledObjectTypes;

	private Dictionary<string, List<GameObject>> pool;

	void Awake()
	{
		// set up singleton reference
		if (shared == null)
		{
			shared = this;
		}
	}

	void Start()
	{
		// set up the pool and instantiate all initial objects
		pool = new Dictionary<string, List<GameObject>>();
		for (int i = 0; i < pooledObjectTypes.Length; i++)
		{
			PooledObject pooledObjectType = pooledObjectTypes[i];
			pool.Add(pooledObjectType.objectToPool.name, new List<GameObject>());
			for (int i2 = 0; i2 < pooledObjectType.pooledAmount; i2++)
			{
				AddPooledObject(pooledObjectType.objectToPool);
			}
		}
	}

	GameObject AddPooledObject(Object newObject)
	{
		GameObject pooledObject = Instantiate(newObject) as GameObject;
		if (shared.gameObject.transform != null)
		{
			pooledObject.transform.parent = shared.gameObject.transform;
		}
		pooledObject.SetActive(false);
		pool[newObject.name].Add(pooledObject);
		return pooledObject;
	}

	public void DisableAllPooledObjects()
	{
		foreach(string key in pool.Keys)
		{
		    foreach(GameObject pooledObject in pool[key])
		    {
		    	pooledObject.SetActive(false);
		    }
		}
	}

	public List<GameObject> GetPooledObjects(Object gameObject)
	{
		if (!pool.ContainsKey(gameObject.name))
		{
			return null;
		}
		return pool[gameObject.name];
	}

	public GameObject GetPooledObject(Object gameObject)
	{
		// return a regular non-pooled version if there is no pool for this type
		List<GameObject> pooledObjects = GetPooledObjects(gameObject);
		if (pooledObjects == null)
		{
			Debug.Log("Returning non-pooled instance of: " + gameObject.name);
			return (Instantiate(gameObject) as GameObject);
		}

		// return first available inactive pooled object
		for (int i = 0; i < pooledObjects.Count; i++)
		{
			if (!pooledObjects[i].activeInHierarchy)
			{
				return pooledObjects[i];
			}
		}

		// grow the pool if needed and return the new object
		for (int i = 0; i < pooledObjectTypes.Length; i++)
		{
			if (pooledObjectTypes[i].objectToPool.name == gameObject.name)
			{
				if (pooledObjectTypes[i].canGrow)
				{
					return AddPooledObject(gameObject);
				}
				else
				{
					return pooledObjects[0];
				}
			}
		}
		
		// return null if none was created
		// this should be handled by the caller script
		return null;
	}
}