Click or drag to resize

ComponentPool Class

[This is preliminary documentation and is subject to change.]

A MonoBehaviour component that provides pooling of many instances of a particular Component, allowing the components to be recycled and reused.
Inheritance Hierarchy
SystemObject
  UnityEngineObject
    UnityEngineComponent
      UnityEngineBehaviour
        UnityEngineMonoBehaviour
          Umbrace.Unity.PurePoolPoolBaseType, Component, ComponentPoolSettings
            Umbrace.Unity.PurePoolComponentPool

Namespace:  Umbrace.Unity.PurePool
Assembly:  Umbrace.Unity.PurePool (in Umbrace.Unity.PurePool.dll) Version: 0.0.0.0 (1.0.0.0)
Syntax
C#
public class ComponentPool : PoolBase<Type, Component, ComponentPoolSettings>, 
	IComponentPoolSettings, ISharedPoolSettings<Type>, ISharedPoolSettings

The ComponentPool type exposes the following members.

Constructors
  NameDescription
Public methodComponentPool
Initializes a new instance of the ComponentPool class
Top
Properties
  NameDescription
Public propertyAdditionalComponentTypes
Gets a collection of additional component types that should exist on the pooled object.
Public propertyCanAcquire

Gets a value indicating whether an instance can be acquired from the pool.

An instance can be acquired when the pool contains at least one instance, or when InstantiateWhenEmpty is .

(Inherited from PoolBaseTSource, TInstance, TSettings.)
Public propertyCount
Gets the number of objects currently contained by the pool.
(Inherited from PoolBaseTSource, TInstance, TSettings.)
Public propertyDefinition
Gets the settings that were used to initialise this pool.
(Inherited from PoolBaseTSource, TInstance, TSettings.)
Public propertyDontDestroyOnLoad
Gets or sets a value indicating whether the pool should persist between scene changes.
(Inherited from PoolBaseTSource, TInstance, TSettings.)
Public propertyEnabled
Gets or sets a value indicating whether pooling is enabled.
(Inherited from PoolBaseTSource, TInstance, TSettings.)
Public propertyInitialiseOnStart
Gets or sets a value indicating whether to initialise the pool in the MonoBehaviour Start method. Cannot be set once the pool has been initialised.
(Inherited from PoolBaseTSource, TInstance, TSettings.)
Public propertyInitialSize
Gets or sets the initial size of the pool. Cannot be set once the pool has been initialised.
(Inherited from PoolBaseTSource, TInstance, TSettings.)
Public propertyInstantiateWhenEmpty
Gets or sets a value indicating whether to instantiate a new object when the pool is empty, and an attempt is made to acquire from the pool.
(Inherited from PoolBaseTSource, TInstance, TSettings.)
Public propertyIsEmpty
Gets a value indicating whether the pool is empty and contains no objects.
(Inherited from PoolBaseTSource, TInstance, TSettings.)
Public propertyIsInitialised
Gets a value indicating whether the pool has been initialised.
(Inherited from PoolBaseTSource, TInstance, TSettings.)
Public propertyItems
Gets a list of items currently contained by the pool.
(Inherited from PoolBaseTSource, TInstance, TSettings.)
Public propertyLogMessages
Gets or sets the level of log messaging that the pool will output.
(Inherited from PoolBaseTSource, TInstance, TSettings.)
Public propertyMaximumSize
Gets or sets the maximum size of the pool, which is the maximum number of objects it can contain.
(Inherited from PoolBaseTSource, TInstance, TSettings.)
Public propertyNotificationMode
Gets or sets the modes in which pooled objects are notified of their acquisition from, and release to, the pool.
(Overrides PoolBaseTSource, TInstance, TSettingsNotificationMode.)
Protected propertyPool
When implemented in a derived class, gets or sets the internal object pool.
(Overrides PoolBaseTSource, TInstance, TSettingsPool.)
Public propertyRecordStatistics
Gets or sets a value indicating whether to record pool statistics.
(Inherited from PoolBaseTSource, TInstance, TSettings.)
Public propertyReparentPooledObjects
Gets or sets a value indicating whether to re-parent the pooled objects to the pool's Transform, after the objects are returned to the pool.
(Overrides PoolBaseTSource, TInstance, TSettingsReparentPooledObjects.)
Public propertySource
Gets or sets the source object that will be pooled.
(Inherited from PoolBaseTSource, TInstance, TSettings.)
Public propertyStatistics
Gets an object containing general operational statistics about the pool.
(Inherited from PoolBaseTSource, TInstance, TSettings.)
Public propertyWarnOnDestroy
Gets or sets a value indicating whether to log a warning message when a poolable object is destroyed (either inside of the pool, or while in use).
(Overrides PoolBaseTSource, TInstance, TSettingsWarnOnDestroy.)
Top
Methods
  NameDescription
Public methodAcquire
Acquires an instance of the component.
(Overrides PoolBaseTSource, TInstance, TSettingsAcquire.)
Public methodAcquire(Transform)
Acquires an instance of the component, and sets the parent transform of its GameObject.
Public methodAcquire(Vector3, Quaternion)
Acquires an instance of the component, and sets the position and rotation of its GameObject.
Public methodAcquire(Transform, Vector3, Quaternion)
Acquires an instance of the component, and sets the parent transform, position and rotation of its GameObject.
Public methodAcquireT
Acquires an instance of the component.
Public methodAcquireT(Transform)
Acquires an instance of the component, and sets the parent transform of its GameObject.
Public methodAcquireT(Vector3, Quaternion)
Acquires an instance of the component, and sets the position and rotation of its GameObject.
Public methodAcquireT(Transform, Vector3, Quaternion)
Acquires an instance of the component, and sets the parent transform, position and rotation of its GameObject.
Protected methodCanInitialise
When overridden in a derived class, determines whether the pool can be initialised.
(Overrides PoolBaseTSource, TInstance, TSettingsCanInitialise.)
Public methodClear
Clears the pool, emptying it of all pooled objects.
(Inherited from PoolBaseTSource, TInstance, TSettings.)
Protected methodCreateInternalPool
When implemented in a derived class, creates the internal object pool.
(Overrides PoolBaseTSource, TInstance, TSettingsCreateInternalPool.)
Protected methodCreateSettingsClone
When implemented in a derived class, creates a new instance of TSettings that is an exact copy of the specified settings.
(Overrides PoolBaseTSource, TInstance, TSettingsCreateSettingsClone(TSettings).)
Protected methodDestroyInstance
When implemented in a derived class, destroys the specified instance.
(Overrides PoolBaseTSource, TInstance, TSettingsDestroyInstance(TInstance).)
Public methodEquals (Inherited from Object.)
Public methodFill
Fills the pool, populating it with pooled objects until it reaches the maximum pool size.
(Inherited from PoolBaseTSource, TInstance, TSettings.)
Protected methodFinalize (Inherited from Object.)
Public methodGetHashCode (Inherited from Object.)
Public methodGetItems (Inherited from PoolBaseTSource, TInstance, TSettings.)
Public methodGetType (Inherited from Object.)
Public methodGrow
Increases the number of objects contained by the pool by the specified amount.
(Inherited from PoolBaseTSource, TInstance, TSettings.)
Public methodInitialise
Initialises the pool, populating it with the initial number of objects.
(Overrides PoolBaseTSource, TInstance, TSettingsInitialise.)
Public methodInitialise(TSettings)
Initialises the pool, populating it with the initial number of objects.
(Inherited from PoolBaseTSource, TInstance, TSettings.)
Protected methodMemberwiseClone (Inherited from Object.)
Protected methodOnCanAcquireChanged
Raises the CanAcquireChanged event.
(Inherited from PoolBaseTSource, TInstance, TSettings.)
Protected methodOnCountChanged
Raises the CountChanged event.
(Inherited from PoolBaseTSource, TInstance, TSettings.)
Protected methodOnDestroy (Overrides PoolBaseTSource, TInstance, TSettingsOnDestroy.)
Protected methodOnDestroyed
Raises the Destroyed event.
(Inherited from PoolBaseTSource, TInstance, TSettings.)
Protected methodOnInitialised
Raises the Initialised event.
(Inherited from PoolBaseTSource, TInstance, TSettings.)
Protected methodOnObjectAcquired
Raises the ObjectAcquired event.
(Overrides PoolBaseTSource, TInstance, TSettingsOnObjectAcquired(TInstance, Boolean).)
Protected methodOnObjectDestroyed
Raises the ObjectDestroyed event.
(Overrides PoolBaseTSource, TInstance, TSettingsOnObjectDestroyed(TInstance).)
Protected methodOnObjectInstantiated
Raises the ObjectInstantiated event.
(Overrides PoolBaseTSource, TInstance, TSettingsOnObjectInstantiated(TInstance).)
Protected methodOnObjectReleased
Raises the ObjectReleased event.
(Overrides PoolBaseTSource, TInstance, TSettingsOnObjectReleased(TInstance, Boolean).)
Public methodRelease
Releases an instance back to the pool.
(Inherited from PoolBaseTSource, TInstance, TSettings.)
Public methodSetSize
Sets the number of objects contained by the pool, either destroying excess pooled objects, or instantiating new ones.
(Inherited from PoolBaseTSource, TInstance, TSettings.)
Public methodShrink
Decreases the number of objects contained by the pool by the specified amount.
(Inherited from PoolBaseTSource, TInstance, TSettings.)
Protected methodStart (Inherited from PoolBaseTSource, TInstance, TSettings.)
Public methodToString (Inherited from Object.)
Public methodTryAcquire(Component)
Acquires an instance of the component.
(Overrides PoolBaseTSource, TInstance, TSettingsTryAcquire(TInstance).)
Public methodTryAcquire(Transform, Component)
Acquires an instance of the component, and sets the parent transform of its GameObject.
Public methodTryAcquire(Vector3, Quaternion, Component)
Acquires an instance of the component, and sets the position and rotation of its GameObject.
Public methodTryAcquire(Transform, Vector3, Quaternion, Component)
Acquires an instance of the component, and sets the parent transform, position and rotation of its GameObject.
Public methodTryAcquireT(T)
Acquires an instance of the component.
Public methodTryAcquireT(Transform, T)
Acquires an instance of the component, and sets the parent transform of its GameObject.
Public methodTryAcquireT(Vector3, Quaternion, T)
Acquires an instance of the component, and sets the position and rotation of its GameObject.
Public methodTryAcquireT(Transform, Vector3, Quaternion, T)
Acquires an instance of the component, and sets the parent transform, position and rotation of its GameObject.
Top
Events
  NameDescription
Public eventCanAcquireChanged
Occurs when the value of CanAcquire changes.
(Inherited from PoolBaseTSource, TInstance, TSettings.)
Public eventCountChanged
Occurs when Count changes.
(Inherited from PoolBaseTSource, TInstance, TSettings.)
Public eventDestroyed
Occurs when the pool is destroyed.
(Inherited from PoolBaseTSource, TInstance, TSettings.)
Public eventInitialised
Occurs when the pool is initialised.
(Inherited from PoolBaseTSource, TInstance, TSettings.)
Public eventObjectAcquired
Occurs when an instance of the source object is acquired from the pool.
(Inherited from PoolBaseTSource, TInstance, TSettings.)
Public eventObjectDestroyed
Occurs when an instance of the source object is destroyed.
(Inherited from PoolBaseTSource, TInstance, TSettings.)
Public eventObjectInstantiated
Occurs when a new instance of the source object is instantiated.
(Inherited from PoolBaseTSource, TInstance, TSettings.)
Public eventObjectReleased
Occurs when an instance of the source object is released back to the pool.
(Inherited from PoolBaseTSource, TInstance, TSettings.)
Top
Fields
  NameDescription
Public fieldObjectAcquiredEvent
Occurs when an instance of the component is acquired from the pool.
Public fieldObjectDestroyedEvent
Occurs when an instance of the component is destroyed.
Public fieldObjectInstantiatedEvent
Occurs when a new instance of the component is instantiated.
Public fieldObjectReleasedEvent
Occurs when an instance of the component is released back to the pool.
Top
Remarks

By virtue of being serialisable, ComponentPool can survive an assembly reload caused by live recompilation inside of the Unity editor.

To use the ComponentPool, add a new instance of the component to a GameObject, and then set the properties to appropriate values. Once all properties have been set, invoke the Initialise method. A pool cannot be used without being initialised.

Caution note Caution

Unlike prefabs, components are difficult to pool correctly. As the components may have been used for any other purpose previously, there is no simple way of knowing which properties should be reset, and no easy way of resetting all properties.

It's therefore very important when you pool a component that you take great care to ensure all properties that you plan on changing are reset. If necessary, you should implement a component deriving from MonoBehaviour which implements the IPoolable interface, and use this component to reset built-in Unity components.

Examples
C#
// Create the pool as a component on a game object.
var pool = gameObject.AddComponent<ComponentPool>();

// Set up the pool's properties.
pool.Source = typeof(AudioSource);
pool.InitialSize = 50;
pool.MaximumSize = 200;
pool.InstantiateWhenEmpty = true;
pool.NotificationMode = NotificationMode.Interface;
pool.LogMessages = LogLevel.Warning;

// Initialise the pool. It will contain 50 objects.
pool.Initialise();

// Acquire one of the 50 objects from the pool. The Acquire method can be used safely if InstantiateWhenEmpty is true, or if a check is made to CanAcquire beforehand.
AudioSource instance = pool.Acquire<AudioSource>();

// Acquire one of the 49 remaining objects from the pool. TryAcquire can be used safely even when InstantiateWhenEmpty is false.
AudioSource secondInstance;
if (pool.TryAcquire(out secondInstance)) {
    // Release the object back to the pool. It now contains 49 objects again.
    pool.Release(secondInstance);
}

// Release the object back to the pool. It now contains 50 objects.
pool.Release(instance);
See Also