Generic object pooler for Unity
The simpliest, easiest and cleanest way to do object pooling in Unity.
Instructions:
- Download the PoolingSystem folder, put it inside your Unity Project.
- Create an empty gameObject that will hold the PoolingSystem.cs script.
- Attach the GenericPoolableObject.cs to the prefab that you want to pool.
- Put that prefab to a folder named "PooledObjects" under "Resource" folder
- If you want to spawn the prefab simply call
IPooler.OnPoolRequest?.Invoke("nameOfPrefab", vector3Position)
<- the first parameter is the name of the prefab, make sure they have the same name. Second parameter is a Vector3 position where you want to spawn the object. - If you want reference to the object after spawning you can do this:
var objectToSpawn = IPooler.OnPoolRequest?.Invoke("nameOfPrefab", vector3Position);
objectToSpawn.DoSomething();
That's it! Enjoy!