GameObjectPool Class |
[This is preliminary documentation and is subject to change.]
Namespace: Umbrace.Unity.PurePool
public class GameObjectPool : PoolBase<GameObject, GameObject, GameObjectPoolSettings>, IGameObjectPoolSettings, ISharedPoolSettings<GameObject>, ISharedPoolSettings
The GameObjectPool type exposes the following members.
Name | Description | |
---|---|---|
GameObjectPool | Initializes a new instance of the GameObjectPool class |
Name | Description | |
---|---|---|
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 from the pool.
(Inherited from PoolBaseTSource, TInstance, TSettings.) | |
Acquire(Transform) |
Acquires an instance of the source object, and sets its parent transform.
| |
Acquire(Transform, Boolean) |
Acquires an instance of the source object, and sets its parent transform.
| |
Acquire(Vector3, Quaternion) |
Acquires an instance of the source object, and sets its position and rotation.
| |
Acquire(Vector3, Quaternion, Transform) |
Acquires an instance of the source object, and sets its parent transform, position and rotation.
| |
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(TInstance) |
Acquires an instance from the pool.
(Inherited from PoolBaseTSource, TInstance, TSettings.) | |
TryAcquire(Transform, GameObject) |
Acquires an instance of the source object, and sets its parent transform.
| |
TryAcquire(Transform, Boolean, GameObject) |
Acquires an instance of the source object, and sets its parent transform.
| |
TryAcquire(Vector3, Quaternion, GameObject) |
Acquires an instance of the source object, and sets its position and rotation.
| |
TryAcquire(Vector3, Quaternion, Transform, GameObject) |
Acquires an instance of the source object, and sets its parent transform, position and rotation.
|
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 source object is acquired from the pool.
| |
ObjectDestroyedEvent |
Occurs when an instance of the source object is destroyed.
| |
ObjectInstantiatedEvent |
Occurs when a new instance of the source object is instantiated.
| |
ObjectReleasedEvent |
Occurs when an instance of the source object is released back to the pool.
|
By virtue of being serialisable, GameObjectPool can survive an assembly reload caused by live recompilation inside of the Unity editor.
To use the GameObjectPool, 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.
// Create the pool as a component on a game object. var pool = gameObject.AddComponent<GameObjectPool>(); // Set up the pool's properties. pool.SourceObject = prefab; 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. GameObject instance = pool.Acquire(); // Acquire one of the 49 remaining objects from the pool. TryAcquire can be used safely even when InstantiateWhenEmpty is false. GameObject 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);