Skip to content

Object Pooling

Aidan Mathieu edited this page Dec 18, 2024 · 1 revision

A script objectPooling and object have been made for pooling objects to prevent items that are constantly created in the factory to need to be constantly deleted and initiated which can cause performance issues.

Instantiating Objects: The object that normally spawns objects into the game now calls the spawnObject method which takes the same parameters as the built in Instantiate method. This method then looks to see if pools for that objects exists. If they do not, it creates both an inactive and active pool based on that object's name. Now when trying to spawn an object it looks to see if there are any inactive copies of that object in that object's inactive pool. If there are objects in the inactive pool, it sets that object's position and rotation based on the method's parameters, makes it active, and puts it into the active pool. If there are no objects in the inactive pool, a new object is instantiated and placed into the active pool.

Deleting Objects: Now instead of deleting an object, the storeObject script can be used instead. This script will inactivate the object and add it to that object's inactive pool for the spawnObject script to pull from.

Pre-instantiating Objects: Using the PoolObjects script, we can now also pre-instantiate objects for example during a loading sequence for a level we can pool many inactive objects within inactive pools that we expect to be used during the level. This way objects will be instantiated less while the player is actually playing the level which could improve performance.

Clone this wiki locally