Click or drag to resize

ObjectPoolController Class

A static class used to create and destroy poolable objects.
Inheritance Hierarchy
SystemObject
  (Default Namespace)ObjectPoolController

Namespace:  (Default Namespace)
Assembly:  AudioToolkit (in AudioToolkit.dll) Version: 8.2.0.0 (8.2.0.0)
Syntax
C#
public static class ObjectPoolController

The ObjectPoolController type exposes the following members.

Properties
  NameDescription
Public propertyStatic memberisDuringPreload
Top
Methods
  NameDescription
Public methodStatic memberDestroy
Destroys the specified game object, respectively sets the object inactive and adds it to the pool.
Public methodStatic memberInstantiate(GameObject)
Retrieves an instance of the specified prefab. Either returns a new instance or it claims an instance from the pool.
Public methodStatic memberInstantiate(GameObject, Vector3, Quaternion)
Retrieves an instance of the specified prefab. Either returns a new instance or it claims an instance from the pool.
Public methodStatic memberInstantiateWithoutPool(GameObject)
Instantiates the specified prefab without using pooling. from the pool.
Public methodStatic memberInstantiateWithoutPool(GameObject, Vector3, Quaternion)
Instantiates the specified prefab without using pooling. from the pool.
Public methodStatic memberPreload
Preloads as many instances to the pool so that there are at least as many as specified in preloadCount.
Top
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.

Examples
How to set up a prefab for pooling:
  1. 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.
  2. Replace all Instantiate( myPrefab ) calls with ObjectPoolController.Instantiate( myPrefab )
  3. Replace all Destroy( myObjectInstance ) calls with ObjectPoolController.Destroy( myObjectInstance )
Attention: Be aware that:
See Also