ComponentPool Class |
[This is preliminary documentation and is subject to change.]
Namespace: Umbrace.Unity.PurePool
public class ComponentPool : PoolBase<Type, Component, ComponentPoolSettings>, IComponentPoolSettings, ISharedPoolSettings<Type>, ISharedPoolSettings
The ComponentPool type exposes the following members.
Name | Description | |
---|---|---|
ComponentPool | Initializes a new instance of the ComponentPool class |
Name | Description | |
---|---|---|
AdditionalComponentTypes |
Gets a collection of additional component types that should exist on the pooled object.
| |
CanAcquire | 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 . | |
Count |
Gets the number of objects currently contained by the pool.
(Inherited from PoolBaseTSource, TInstance, TSettings.) | |
Definition |
Gets the settings that were used to initialise this pool.
(Inherited from PoolBaseTSource, TInstance, TSettings.) | |
DontDestroyOnLoad |
Gets or sets a value indicating whether the pool should persist between scene changes.
(Inherited from PoolBaseTSource, TInstance, TSettings.) | |
Enabled |
Gets or sets a value indicating whether pooling is enabled.
(Inherited from PoolBaseTSource, TInstance, TSettings.) | |
InitialiseOnStart |
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.) | |
InitialSize |
Gets or sets the initial size of the pool.
Cannot be set once the pool has been initialised.
(Inherited from PoolBaseTSource, TInstance, TSettings.) | |
InstantiateWhenEmpty |
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.) | |
IsEmpty |
Gets a value indicating whether the pool is empty and contains no objects.
(Inherited from PoolBaseTSource, TInstance, TSettings.) | |
IsInitialised |
Gets a value indicating whether the pool has been initialised.
(Inherited from PoolBaseTSource, TInstance, TSettings.) | |
Items |
Gets a list of items currently contained by the pool.
(Inherited from PoolBaseTSource, TInstance, TSettings.) | |
LogMessages |
Gets or sets the level of log messaging that the pool will output.
(Inherited from PoolBaseTSource, TInstance, TSettings.) | |
MaximumSize |
Gets or sets the maximum size of the pool, which is the maximum number of objects it can contain.
(Inherited from PoolBaseTSource, TInstance, TSettings.) | |
NotificationMode |
Gets or sets the modes in which pooled objects are notified of their acquisition from, and release to, the pool.
(Overrides PoolBaseTSource, TInstance, TSettingsNotificationMode.) | |
Pool |
When implemented in a derived class, gets or sets the internal object pool.
(Overrides PoolBaseTSource, TInstance, TSettingsPool.) | |
RecordStatistics |
Gets or sets a value indicating whether to record pool statistics.
(Inherited from PoolBaseTSource, TInstance, TSettings.) | |
ReparentPooledObjects |
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.) | |
Source |
Gets or sets the source object that will be pooled.
(Inherited from PoolBaseTSource, TInstance, TSettings.) | |
Statistics |
Gets an object containing general operational statistics about the pool.
(Inherited from PoolBaseTSource, TInstance, TSettings.) | |
WarnOnDestroy |
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.) |
Name | Description | |
---|---|---|
Acquire |
Acquires an instance of the component.
(Overrides PoolBaseTSource, TInstance, TSettingsAcquire.) | |
Acquire(Transform) |
Acquires an instance of the component, and sets the parent transform of its GameObject.
| |
Acquire(Vector3, Quaternion) |
Acquires an instance of the component, and sets the position and rotation of its GameObject.
| |
Acquire(Transform, Vector3, Quaternion) |
Acquires an instance of the component, and sets the parent transform, position and rotation of its GameObject.
| |
AcquireT |
Acquires an instance of the component.
| |
AcquireT(Transform) |
Acquires an instance of the component, and sets the parent transform of its GameObject.
| |
AcquireT(Vector3, Quaternion) |
Acquires an instance of the component, and sets the position and rotation of its GameObject.
| |
AcquireT(Transform, Vector3, Quaternion) |
Acquires an instance of the component, and sets the parent transform, position and rotation of its GameObject.
| |
CanInitialise |
When overridden in a derived class, determines whether the pool can be initialised.
(Overrides PoolBaseTSource, TInstance, TSettingsCanInitialise.) | |
Clear |
Clears the pool, emptying it of all pooled objects.
(Inherited from PoolBaseTSource, TInstance, TSettings.) | |
CreateInternalPool |
When implemented in a derived class, creates the internal object pool.
(Overrides PoolBaseTSource, TInstance, TSettingsCreateInternalPool.) | |
CreateSettingsClone |
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).) | |
DestroyInstance |
When implemented in a derived class, destroys the specified instance.
(Overrides PoolBaseTSource, TInstance, TSettingsDestroyInstance(TInstance).) | |
Equals | (Inherited from Object.) | |
Fill |
Fills the pool, populating it with pooled objects until it reaches the maximum pool size.
(Inherited from PoolBaseTSource, TInstance, TSettings.) | |
Finalize | (Inherited from Object.) | |
GetHashCode | (Inherited from Object.) | |
GetItems |
Gets a list of items currently contained by the pool, and stores them in the specified ListT.
(Inherited from PoolBaseTSource, TInstance, TSettings.) | |
GetType | (Inherited from Object.) | |
Grow |
Increases the number of objects contained by the pool by the specified amount.
(Inherited from PoolBaseTSource, TInstance, TSettings.) | |
Initialise |
Initialises the pool, populating it with the initial number of objects.
(Overrides PoolBaseTSource, TInstance, TSettingsInitialise.) | |
Initialise(TSettings) |
Initialises the pool, populating it with the initial number of objects.
(Inherited from PoolBaseTSource, TInstance, TSettings.) | |
MemberwiseClone | (Inherited from Object.) | |
OnCanAcquireChanged |
Raises the CanAcquireChanged event.
(Inherited from PoolBaseTSource, TInstance, TSettings.) | |
OnCountChanged |
Raises the CountChanged event.
(Inherited from PoolBaseTSource, TInstance, TSettings.) | |
OnDestroy | (Overrides PoolBaseTSource, TInstance, TSettingsOnDestroy.) | |
OnDestroyed |
Raises the Destroyed event.
(Inherited from PoolBaseTSource, TInstance, TSettings.) | |
OnInitialised |
Raises the Initialised event.
(Inherited from PoolBaseTSource, TInstance, TSettings.) | |
OnObjectAcquired |
Raises the ObjectAcquired event.
(Overrides PoolBaseTSource, TInstance, TSettingsOnObjectAcquired(TInstance, Boolean).) | |
OnObjectDestroyed |
Raises the ObjectDestroyed event.
(Overrides PoolBaseTSource, TInstance, TSettingsOnObjectDestroyed(TInstance).) | |
OnObjectInstantiated |
Raises the ObjectInstantiated event.
(Overrides PoolBaseTSource, TInstance, TSettingsOnObjectInstantiated(TInstance).) | |
OnObjectReleased |
Raises the ObjectReleased event.
(Overrides PoolBaseTSource, TInstance, TSettingsOnObjectReleased(TInstance, Boolean).) | |
Release |
Releases an instance back to the pool.
(Inherited from PoolBaseTSource, TInstance, TSettings.) | |
SetSize |
Sets the number of objects contained by the pool, either destroying excess pooled objects, or instantiating new ones.
(Inherited from PoolBaseTSource, TInstance, TSettings.) | |
Shrink |
Decreases the number of objects contained by the pool by the specified amount.
(Inherited from PoolBaseTSource, TInstance, TSettings.) | |
Start | (Inherited from PoolBaseTSource, TInstance, TSettings.) | |
ToString | (Inherited from Object.) | |
TryAcquire(Component) |
Acquires an instance of the component.
(Overrides PoolBaseTSource, TInstance, TSettingsTryAcquire(TInstance).) | |
TryAcquire(Transform, Component) |
Acquires an instance of the component, and sets the parent transform of its GameObject.
| |
TryAcquire(Vector3, Quaternion, Component) |
Acquires an instance of the component, and sets the position and rotation of its GameObject.
| |
TryAcquire(Transform, Vector3, Quaternion, Component) |
Acquires an instance of the component, and sets the parent transform, position and rotation of its GameObject.
| |
TryAcquireT(T) |
Acquires an instance of the component.
| |
TryAcquireT(Transform, T) |
Acquires an instance of the component, and sets the parent transform of its GameObject.
| |
TryAcquireT(Vector3, Quaternion, T) |
Acquires an instance of the component, and sets the position and rotation of its GameObject.
| |
TryAcquireT(Transform, Vector3, Quaternion, T) |
Acquires an instance of the component, and sets the parent transform, position and rotation of its GameObject.
|
Name | Description | |
---|---|---|
CanAcquireChanged |
Occurs when the value of CanAcquire changes.
(Inherited from PoolBaseTSource, TInstance, TSettings.) | |
CountChanged |
Occurs when Count changes.
(Inherited from PoolBaseTSource, TInstance, TSettings.) | |
Destroyed |
Occurs when the pool is destroyed.
(Inherited from PoolBaseTSource, TInstance, TSettings.) | |
Initialised |
Occurs when the pool is initialised.
(Inherited from PoolBaseTSource, TInstance, TSettings.) | |
ObjectAcquired |
Occurs when an instance of the source object is acquired from the pool.
(Inherited from PoolBaseTSource, TInstance, TSettings.) | |
ObjectDestroyed |
Occurs when an instance of the source object is destroyed.
(Inherited from PoolBaseTSource, TInstance, TSettings.) | |
ObjectInstantiated |
Occurs when a new instance of the source object is instantiated.
(Inherited from PoolBaseTSource, TInstance, TSettings.) | |
ObjectReleased |
Occurs when an instance of the source object is released back to the pool.
(Inherited from PoolBaseTSource, TInstance, TSettings.) |
Name | Description | |
---|---|---|
ObjectAcquiredEvent |
Occurs when an instance of the component is acquired from the pool.
| |
ObjectDestroyedEvent |
Occurs when an instance of the component is destroyed.
| |
ObjectInstantiatedEvent |
Occurs when a new instance of the component is instantiated.
| |
ObjectReleasedEvent |
Occurs when an instance of the component is released back to the pool.
|
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 |
---|
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. |
// 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);