The Easiest Pooling System you could ever see

pooling system in Unity is a performance optimization technique used to manage the creation, reuse, and destruction of objects in a game or application. Instead of creating and destroying objects (such as game objects or prefabs) frequently, which can be computationally expensive and lead to memory fragmentation, a pooling system keeps a pool (collection) of inactive objects ready to be reused.

Getting Started

[SerializeField]
GameObject cube; //you need reference of your prefab

private async void SpawnCube() 
{
		//this is how you instantiate from pool
    GameObject instance = cube.GetPool(new Vector3(0,0,0), Quaternion.identity);

    await Task.Delay(3000);
    //this is how you return your instance to pool
    instance.Release();
    
    //do i need to setup in editor? NO
    //i can call GetPool without setup? you sure? YES
}

Warming Up

by default, you don’t need to do this, but in case you’re working on very big pool, you’ll probably need this to reduce performance spike at gameplay time

[SerializeField]
GameObject style1;
[SerializeField]
MeshRenderer style2;
[SerializeField]
CustomClass style3;

private void Start()
{
    int poolCount = 5; //how much is preserved in pool
    int poolLimit = 10; //how much pool should store, more than this will be destroyed
    bool isNonLazy = false; //should we spawn `poolCount` immidiately?
    style1.Register(poolCount,poolLimit,isNonLazy);
    style2.Register(poolCount,poolLimit,isNonLazy);
    style3.Register(poolCount,poolLimit,isNonLazy);
}

Pooling

To execute pooling, please follows this steps

[SerializeField]
GameObject style1;
[SerializeField]
MeshRenderer style2;
[SerializeField]
CustomClass style3;

//Note:GetPool(Position, Quaternion, Parent)

public async void StyleGO() 
{
    var pos = GetSpawnPos();
    GameObject instance = style1.GetPool(pos, Quaternion.identity , null);

    await Task.Delay(3000);
    instance.Release();
}

public async void StyleComponent()
{
    var pos = GetSpawnPos();
    var instance = style2.GetPool(pos , Quaternion.identity, null);

    await Task.Delay(3000);
    instance.Release();
}

public async void StyleCustom()
{
    var pos = GetSpawnPos();
    AutoDestroyComponent instance = style3.GetPool(pos, Quaternion.identity, null);

    await Task.Delay(3000);
    instance.Release();
}

To enable pooling for custom classes, the class has to implement GrandPooling.IObjectPool<T> interface

public class CustomClass : MonoBehaviour, IObjectPool<CustomClass>
{
}

NOTE : It’s also support same Type of Multiple Object, as long as you give different Prefab then it'll handle the Pool automatically.