Instantiating and Destroying objects in the scene during runtine is expensive in terms of memory management. Object pooling is a solution to this problem which allows us to save memory and improve performance.
- All objects are loaded into a pool "once" during initialization
- These objects can then be retrieved from the pool when required
- And returned back to the pool when not required
Advantage: Using this approach allows us to completely abandon calling Instantiate and Destroy funcations during runtime.
- Attach the
EasyPool.cs
script to any gameobject in the scene (it's a singleton, but still, just ensure there is only one instance in the scene to avoid any odd scenario). - Add the prefabs of your choice in the PrefabsToPool list.
- Mention the pool size (default size is 25).
- Check Randomise if you want to retrive objects at rondom from the pool. If unchecked, the objects will be obtained sequentialy similar to a Queue.
- To retrive, Call
EasyPool.Instance.GetObj()
in any other scipt where you need to load objects.
Note: The game object obtained in the above step is disabled by default, enable it by calling the SetActive() function on it.
- To return, Call
EasyPool.Instance.ReturnObj(obj)
where obj is the object you no longer need.
That's it!
The "EasyPoolExample" scene inside the Scenes folder shows the implementation with an ExampleSpawn.cs
script included for your assistance.