Game object pooling can be used to increase the performance of your game – learn more about it and when to use it.
Instantiating and destroying game objects can decrease the performance of your game, especially when happening often. This can be reduced by using pooling.
Pooling is a technique that will instantiate a pool of game objects when a level was loaded and only enables and disables them afterwards instead of destroying them and instantiating new objects. Usually, this will help increasing the performance, but it can depend on the use case if it actually helps or has negative effects, since having a lot of game objects alive in a pool (even disabled) also costs memory.
As said, pooling costs memory for holding the game objects, but increases the performance by removing the heavy instantiation and destruction of game objects.
You should use pooling when game objects are often created and destroyed, e.g.:
You shouldn’t use pooling for things like level architecture or procedurally generated levels, since those will most likely only be instantiated once and not destroyed until the level ends.
E.g. the missiles fired by the player’s ship in a vertical space shooter are pooled. The player’s fire rate and the time a missile will be alive result in a maximum of about 15 missiles to be alive at the same time.
The pool size is 20 – this is a good use of a pool, but can also be reduced a bit.
The pool size is 100 – this is a waste of memory.
Game object pools are set up in Base/Control > Game Object Pools.
A game object pool is based on a prefab and instantiates it when a level was loaded. The Start Size of the pool defines the quantity of game objects that will be created at the start of a level. Pools can optionally grow by enabling Can Grow in the pool’s settings – this will add more game objects to the pool when needed and keeps them alive, otherwise game objects exceeding the pool will be destroyed again.
The pooled objects can be used by the prefabs defined in schematics. It’s only used when the prefab is defined as a resource in the schematic’s settings and has using pooling enabled. The prefab in the schematic and the pool must be the same prefab. If that’s the case, spawning and destroying a prefab in the schematic will get a game object from the pool and enable/disable it instead of instantiating and destroying it.
The prefab missile is pooled with a start size of 10 and can grow.
The player’s control is handled by a schematic using the missile prefab as a resource with Use Pooling enabled. The same prefab as the pooled prefab is used, i.e. spawning this prefab will use a pooled game object.
The level starts and 10 missile prefabs are instantiated and disabled immediately. The player fires missiles, they’re placed at the player’s position and enabled (Spawn Prefab node). When they hit a target or leave the screen, they’re disabled (Destroy Prefab node).
If the pool runs out of missiles, new missiles will be instantiated and added to the pool (i.e. they’ll also be enabled/disabled when used).
The Pool component is used to add a game object pool to a scene.
It’ll instantiate the selected pool’s prefab when the level was loaded (or when the component is added to the scene in a running game, e.g. spawning pools via prefabs).
Pools are only used in the scene they where added to, i.e. you need to add them to each scene they should be used in.
© 2015 Gaming is Love e.U.
Disclosure: This site may contain affiliate links, which means I may receive a commission if you click a link and purchase something that I have recommended. While clicking these links won’t cost you any money, they will help me fund my development projects while recommending great assets!