- Add Pool Manager to GameObject.
- The Editor will look like this.
- Check or Uncheck Don't Destroy on Load.
- Add Element to the list.
- Give initial amount.
- Use the following code to spawn. (Note: Prefab should be same in the prefabToSpawn as given in the Pool Manager)
GameObject spawned = PoolManager.Instance.Spawn(prefabToSpawn, Position, Rotation, Parent);
- To Despawn use the following code.
PoolManager.Instance.Despawn(gameObjectToDeSpawn, cleanHierarchy);
- I have Added delegates called on item Spawned and item Despawned.
void Start()
{
PoolManager.Instance.OnSpawned += Func;
PoolManager.Instance.OnDespawned += Func1;
}
void Func(GameObject itemSpawned)
{
// do something
}
void Func1(GameObject itemDespawned)
{
// do something
}
void Destroy()
{
PoolManager.Instance.OnSpawned -= Func;
PoolManager.Instance.OnDespawned -= Func1;
}