Game Dev Cheat Sheet

Object Pool Generator

Generate production-ready Unity object pooling code with configurable pool size, auto-expand, callbacks, and more.

Object pooling is essential for performance in Unity. Instead of Instantiate and Destroy (which trigger garbage collection), you pre-create objects and reuse them. This generator outputs a complete pooling class based on your configuration.

Configuration

Pool type
Auto-expand when empty
Optional features

Generated Code

Uses UnityEngine.Pool.ObjectPool<T>, available in Unity 2021.1 and later.

using UnityEngine;
using UnityEngine.Pool;

public class ProjectilePool : MonoBehaviour
{
    [SerializeField] private GameObject prefab;
    [SerializeField] private int initialSize = 20;

    private ObjectPool<GameObject> pool;

    private void Awake()
    {
        pool = new ObjectPool<GameObject>(
            createFunc: () => Instantiate(prefab),
            actionOnGet: obj => obj.SetActive(true),
            actionOnRelease: obj => obj.SetActive(false),
            actionOnDestroy: obj => Destroy(obj),
            defaultCapacity: 20,
            maxSize: 100
        );

        // Pre-warm the pool
        var prewarm = new GameObject[initialSize];
        for (int i = 0; i < initialSize; i++)
        {
            prewarm[i] = pool.Get();
        }
        for (int i = 0; i < initialSize; i++)
        {
            pool.Release(prewarm[i]);
        }
    }

    public GameObject Get()
    {
        GameObject obj = pool.Get();
        return obj;
    }

    public GameObject Get(Vector3 position, Quaternion rotation)
    {
        GameObject obj = Get();
        obj.transform.SetPositionAndRotation(position, rotation);
        return obj;
    }

    public void Release(GameObject obj)
    {
        pool.Release(obj);
    }
}

Usage Example

How to use the generated pool in your game scripts.

// Getting an object from the pool
[SerializeField] private ProjectilePool pool;

GameObject projectile = pool.Get(transform.position, transform.rotation);

// Returning it when done
pool.Release(projectile);