A static class used to create and destroy poolable objects.
AudioToolkit (in AudioToolkit.dll) Version: 220.127.116.11 (18.104.22.168)
public static class ObjectPoolController
The ObjectPoolController type exposes the following members.
Properties Methods Remarks
What is pooling?
GameObject.Instantiate(...) calls are relatively time expensive. If objects of the same
type are frequently created and destroyed it is good practice to use object pools, particularly on mobile
devices. This can greatly reduce the performance impact for object creation and garbage collection.
How does pooling work?
Instead of actually destroying object instances, they are just set inactive and moved to an object "pool".
If a new object is requested it can then simply be pulled from the pool, instead of creating a new instance.
Awake(), Start() and OnDestroy() are called if objects are retrieved from or moved to the pool like they
were instantiated and destroyed normally.
How to set up a prefab for pooling:
- Add the PoolableObject script component to the prefab to be pooled.
You can set the maximum number of objects to be be stored in the pool from within the inspector.
- Replace all Instantiate( myPrefab ) calls with ObjectPoolController.Instantiate( myPrefab )
- Replace all Destroy( myObjectInstance ) calls with ObjectPoolController.Destroy( myObjectInstance )
Attention: Be aware that:
- All data must get initialized in the Awake() or Start() function
- OnDestroy() will get called a second time once the object really gets destroyed by Unity
- If a poolable objects gets parented to none-poolable object, the parent must
be destroyed using ObjectPoolController.Destroy( ... ) even if it is none-poolable itself.
- If you store a reference to a poolable object then this reference does not evaluate to null after ObjectPoolController.Destroy( ... )
was called like other references to Unity objects normally would. This is because the object still exists - it is just in the pool.
To make sure that a stored reference to a poolable object is still valid you must use PoolableReferenceT.