Object Pooling

Object Pooling is a function to reduce the number of high-load instantiation / destruction by reusing instances of prefab (original object).

Overview

  • Store / retrieve in the pool using the original object as a key
  • GameObject stored in the pool is deactivated and moved to the DontDestroyOnLoad scene
    • It will be destroyed when it is determined to be unnecessary according to the lifetime setting.
  • The GameObject taken out of the pool is activated and moved to the currently active scene (or a child of the specified object).

Stored in the pool in advance

Use AdvancedPooling to store in the pool in advance.

AdvancedPooling asynchronously instantiates and stores in the pool.
Since it will be a heavy load due to a large number of instantiations, it is better to do it on the loading screen etc.

Take out from the pool

Use InstantiateGameObject to take out of the pool.

By turning on “Use Pool”, the instances stored in the pool will be activated and reused.
If the pool is empty, it will be newly instantiated.

If “Use Pool” is turned off, it will be instantiated as usual.
It cannot be changed later so that it can be stored in the pool.

Store in pool

Use DestroyGameObject to store in the pool.

Only GameObjects taken out of the pool using InstantiageGameObject can be stored.
Objects that were originally placed in the scene and objects that are not managed by the pool will be destroyed as usual.

Lifetime

Objects stored in the pool are destroyed when they are deemed unnecessary due to the passage of time.

See LifeTimeFlags for more information.

IPoolCallbackReceiver

An interface that allows you to receive calls back when retrieved / stored from the pool.
When you initialize the state when you retrieve it, set the component that implements this interface to the original GameObject.

See IPoolCallbackReceiver for more information.