Object Pooling, Optimization and Performance in Unity
To prevent Garbage Collector issues (CPU Spikes) in games with many spawning and destroying objects, a method called Object Pooling can be used. Object Pooling refers to creating all necessary objects beforehand and disabling/enabling them when it necessary, instead of instantiating (Instantiate() function) and destroying (Destroy() function) objects during runtime.
This is often done by having an array containing disabled objects that are often used, e.g. bullets in FPS or units in a Strategy game. When a gun is fired, instead of spawning a new bullet and allocating memory for it, we take a game object from the list (object pool), move it to the right position & activate it.
When the bullet collides with something, instead of destroying it, we disable it and add it back our object pool in default state to use it later.
This way we can have many objects appear and disappear, without allocating any memory and causing issues with the garbage collector.
These objects can also be spawned beforehand during a loading screen and kept hidden until needed. This way they won’t cause performance issues when spawned during gameplay.
Pool Boss — the best pooling solution on Unity Asset Store that makes simple to avoid the costly Instantiate() and Destroy() calls that cause performance problems. All types of prefab are supported — even particle systems.
One short line of code can despawn or spawn prefabs, or you can use our included Playmaker scripts!
Pool Kit — Ultimate Object Pool Manager, next generation system for pooling, spawning and despawning with ZERO Garbage Collection which can be up to 77% faster than instantiating (~33% on average) and helps to make your game a smoother experience which is increasingly needed on platforms like mobile, AR and VR!
Pool Kit also thinks about how you work in Unity and comes with its powerhouse of a Spawner. This can be used to create enemies in strategy or tower defense game genres, special effects, weapon systems and more!
Random Object Pooler
Random Object Pooler is improved version of Object Pooling System by Unity Technologies.
Features of Random Object Pooler
- Prefabs Using:
- Single (actual for Testing target prefab; None => Multiple),
- Generation order for Multiple Prefabs:
- In random order,
- In the right order;
- Customizable pool parent & init pooled amount;
- Caching of Control Scripts (MonoBehaviour) for all pool members.
Package is a Part of Unity Assets
Random Object Pooler is available as a part of Complete Projects & Tools: