Simple tu use and foolproof. It uses abstraction to make it almost invisible on your code and easy to reuse it.
- Inherid
PoolableObject
on your spawnable object class:
public class MySpawnable : PoolableObject
{
public override void OnObjectCreated()
{
base.OnObjectCreated();
}
public override void OnGetFromPool()
{
base.OnGetFromPool();
}
public override void OnReleaseToPool()
{
base.OnReleaseToPool();
}
public override void OnDestroyPooledObject()
{
base.OnDestroyPooledObject();
}
}
- Create a prefab of the spawnable object:
- Create a GameObject, attach
ObjectPool
and assign the your spawnable prefab to the variableObject Prefab
:
- Manage the poolable objects through a custom script:
public class MyObjectSpawner : MonoBehaviour
{
[SerializeField] ObjectPool _ObjectPool;
public void SpawnObject()
{
IPoolableObject poolableObject = _ObjectPool.GetObject();
}
public void HideObject(IPoolableObject poolableObject)
{
_ObjectPool.ReleaseObject(poolableObject);
}
}
- 2021.3 or above
- Any pipeline (Build-in, URP, HDRP, etc)
⭐ Star if you like it
❤️️ Follow me for more