Pooling Utility is a simple and powerful pooling system for Unity. It is generic, so any object that implements base classes can be pooled easily.
- Inherit your monobehaviour object from PoolableObject class.
public class MyAwesomeEnemy:PoolableObject
{
//Logic
}
- Inherit a data container from SpawnDataContainer for injecting data to your Monobehaviour class.
public class EnemySpawnData:SpawnDataContainer
{
public Vector3 SpawnPosition;
}
- Use PoolManager to pool your object.
[SerializeField] private MyAwesomeEnemy prefab;
private void Start()
{
PoolManager.GetInstance().GenerateNewPool(prefab);
}
Then you can spawn/despawn your prefab clones from the pool as follows:
//Request spawn
var spawnedObject = PoolManager.RequestSpawnForType(prefab,dataContainer);
//Request despawn
PoolManager.DespawnObject(prefab, spawnedObject);