Ignition / GameObjectPooler
Inherits from MonoBehaviour
Class or component can be found in Assets/Playniax/Framework/Ignition/Framework/Scripts (MonoBehaviour)/GameObjectPooler.cs
Instantiate() and Destroy() are useful and necessary methods during gameplay. Each generally requires minimal CPU time. However, for objects created during gameplay that have a short lifespan and get destroyed in vast numbers per second, the CPU needs to allocate considerably more time. Additionally, Unity uses Garbage Collection to deallocate memory that’s no longer in use. Repeated calls to Destroy() frequently trigger this task, and it has a knack for slowing down CPUs and introducing pauses to gameplay. This behavior is critical in resource-constrained environments such as mobile devices and web builds. Object pooling is where you pre-instantiate all the objects you’ll need at any specific moment before gameplay — for instance, during a loading screen. Instead of creating new objects and destroying old ones during gameplay, your game reuses objects from a 'pool'.
| The object to pre-instantiate.|
| Number of objects to pre-instantiate.|
| Whether to allow the number instantiated objects to grow or not.|
| Whether to hide the objects in the hierarchy or not.|
Example scene can be found in Assets/Playniax/Framework/Ignition/Examples/01 - Framework/GameObjectPooler.unity
Example script can be found in Assets/Playniax/Framework/Ignition/Examples/01 - Framework/Scripts (MonoBehaviour)/GameObjectPooler_Example.cs
public class GameObjectPooler_Example : MonoBehaviour
[Tooltip("Prefab to be used")]
public GameObject prefab;
[Tooltip("Parent to be used")]
public Transform parent;
public Timer timer;
// See if object is available.
var bullet = GameObjectPooler.GetAvailableObject(prefab);
// Was created?
// Override parent.
if (parent) bullet.transform.parent = parent;
// Set position.
bullet.transform.position = transform.position;
// Don't forget to activate.