Leveraging Unity’s ObjectPool class
If you’re using Unity 2021 or later, you have access to a host of new classes that take out some of the boilerplate code we had to write ourselves in the previous sections. Unity’s ObjectPool<T0>
class is a stack of pooled objects that are optimized and managed for reusability – your job is to fill out the pooling actions and Unity takes care of the rest.
Since we’re already familiar with the Object Pool structure, let’s focus on the parameters of the ObjectPool
constructor:
createFunc
is used to create a new instance of the pooled object.actionOnGet
is called when a pooled object is taken out of the pool.actionOnRelease
is called when a pooled object is returned to the pool and can contain resetting or cleanup logic.actionOnDestroy
is called when the pool has reached the maximum size and a pooled object can’t be returned.
Each of these parameters...