Click or drag to resize

SerialisableGenericComponentPoolT Class

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

A generic, serialisable object pool for a Component, where the component type is known at compile time.
Inheritance Hierarchy
SystemObject
  Umbrace.Unity.PurePoolSerialisableObjectPoolT
    Umbrace.Unity.PurePoolSerialisableGenericComponentPoolT
      Umbrace.Unity.PurePoolSerialisableComponentPool

Namespace:  Umbrace.Unity.PurePool
Assembly:  Umbrace.Unity.PurePool (in Umbrace.Unity.PurePool.dll) Version: 0.0.0.0 (1.0.0.0)
Syntax
C#
[SerializableAttribute]
public class SerialisableGenericComponentPool<T> : SerialisableObjectPool<T>
where T : Component

Type Parameters

T
The type of component being pooled.

The SerialisableGenericComponentPoolT type exposes the following members.

Constructors
  NameDescription
Public methodSerialisableGenericComponentPoolT
Initialises a new instance of the SerialisableGenericComponentPoolT class.
Top
Properties
  NameDescription
Public propertyAdditionalComponentTypes
Gets a list of additional component types, that should be added to the same GameObject as the main component type.
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 SerialisableObjectPoolT.)
Public propertyComponentType
Gets the type of the component being pooled.
Public propertyCount
Gets the number of objects currently contained by the pool.
(Inherited from SerialisableObjectPoolT.)
Public propertyInitialSize
Gets or sets the initial size of the pool. Cannot be set once the pool has been initialised.
(Inherited from SerialisableObjectPoolT.)
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 SerialisableObjectPoolT.)
Public propertyIsEmpty
Gets a value indicating whether the pool is empty and contains no objects.
(Inherited from SerialisableObjectPoolT.)
Public propertyIsFull
Gets a value indicating whether the pool is full, and cannot contain any more objects.
(Inherited from SerialisableObjectPoolT.)
Public propertyIsInitialised
Gets a value indicating whether the pool has been initialised.
(Inherited from SerialisableObjectPoolT.)
Public propertyItems
Gets a list of items currently contained by the pool.
(Inherited from SerialisableObjectPoolT.)
Public propertyLogMessages
Gets or sets the level of log messaging that the pool will output.
(Inherited from SerialisableObjectPoolT.)
Public propertyMaximumSize
Gets or sets the maximum size of the pool, which is the maximum number of objects it can contain.
(Inherited from SerialisableObjectPoolT.)
Public propertyNotificationMode
Gets or sets the way in which pooled objects are notified about being acquired from, and returned to, the pool. Cannot be set once the pool has been initialised.
Public propertyParent
Gets or sets the parent transform to which all pooled objects will be parented in the hierarchy, if ReparentPooledObjects is .
Public propertyRecordStatistics
Gets or sets a value indicating whether to record pool statistics.
(Inherited from SerialisableObjectPoolT.)
Protected propertyRefillPoolOnReinitialise
Gets a value indiciating whether to refill the pool with new components after the pool is reinitialised, as happens from deserialisation.
(Overrides SerialisableObjectPoolTRefillPoolOnReinitialise.)
Public propertyReparentPooledObjects
Gets or sets a value indicating whether to re-parent the pooled objects to the Parent transform, after the objects are returned to the pool.
Public propertyStatistics
Gets an object containing general operational statistics about the pool.
(Inherited from SerialisableObjectPoolT.)
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).
Top
Methods
  NameDescription
Public methodAcquire
Acquires an object from the pool.
(Inherited from SerialisableObjectPoolT.)
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 methodClear
Clears the pool, emptying it of all pooled objects.
(Inherited from SerialisableObjectPoolT.)
Public methodContains
Determines whether an instance is in the pool.
(Inherited from SerialisableObjectPoolT.)
Public methodEquals (Inherited from Object.)
Public methodFill
Fills the pool, populating it with pooled objects until it reaches the maximum pool size.
(Inherited from SerialisableObjectPoolT.)
Protected methodFinalize (Inherited from Object.)
Public methodGetHashCode (Inherited from Object.)
Public methodGetItems (Inherited from SerialisableObjectPoolT.)
Protected methodGetObjectFactory
Gets a function used to create new instances of the pooled type. By default, this method uses the public parameterless constructor of type T. This method should be overridden in a subclass if different behaviour is required.
(Overrides SerialisableObjectPoolTGetObjectFactory.)
Public methodGetType (Inherited from Object.)
Public methodInitialise
Initialises the pool, populating it with objects and making it ready for use.
(Inherited from SerialisableObjectPoolT.)
Protected methodInitialise(Boolean)
Initialises the pool, making it ready for use, and optionally populating it with objects.
(Inherited from SerialisableObjectPoolT.)
Protected methodMemberwiseClone (Inherited from Object.)
Protected methodOnAfterDeserialize
Performs actions after the object has been deserialised.
(Overrides SerialisableObjectPoolTOnAfterDeserialize.)
Protected methodOnBeforeSerialize
Performs actions prior to the object being serialised.
(Overrides SerialisableObjectPoolTOnBeforeSerialize.)
Protected methodOnCanAcquireChanged
Raises the CanAcquireChanged event.
(Inherited from SerialisableObjectPoolT.)
Protected methodOnCountChanged
Raises the CountChanged event.
(Inherited from SerialisableObjectPoolT.)
Protected methodOnInitialised
Raises the Initialised event.
(Inherited from SerialisableObjectPoolT.)
Protected methodOnObjectAcquired
Raises the ObjectAcquired event.
(Overrides SerialisableObjectPoolTOnObjectAcquired(T, Boolean).)
Protected methodOnObjectDestroyed
Raises the ObjectDestroyed event.
(Overrides SerialisableObjectPoolTOnObjectDestroyed(T).)
Protected methodOnObjectInstantiated
Raises the ObjectInstantiated event.
(Inherited from SerialisableObjectPoolT.)
Protected methodOnObjectReleased
Raises the ObjectReleased event.
(Overrides SerialisableObjectPoolTOnObjectReleased(T, Boolean).)
Public methodRelease
Releases an object back to the pool.
(Inherited from SerialisableObjectPoolT.)
Protected methodReleaseInternal
Releases an object back to the pool.
(Overrides SerialisableObjectPoolTReleaseInternal(T).)
Public methodRemove
Removes the specified instance from the pool.
(Inherited from SerialisableObjectPoolT.)
Public methodSetSize
Sets the number of objects contained by the pool, either destroying excess pooled objects, or instantiating new ones.
(Inherited from SerialisableObjectPoolT.)
Public methodToString (Inherited from Object.)
Public methodTryAcquire(T)
Acquires an object from the pool.
(Inherited from SerialisableObjectPoolT.)
Public methodTryAcquire(Transform, T)
Acquires an instance of the component, and sets the parent transform of its GameObject.
Public methodTryAcquire(Vector3, Quaternion, T)
Acquires an instance of the component, and sets the position and rotation of its GameObject.
Public methodTryAcquire(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 SerialisableObjectPoolT.)
Public eventCountChanged
Occurs when Count changes.
(Inherited from SerialisableObjectPoolT.)
Public eventInitialised
Occurs when the pool is initialised.
(Inherited from SerialisableObjectPoolT.)
Public eventObjectAcquired
Occurs when an object is acquired from the pool.
(Inherited from SerialisableObjectPoolT.)
Public eventObjectDestroyed
Occurs when an object is destroyed.
(Inherited from SerialisableObjectPoolT.)
Public eventObjectInstantiated
Occurs when a new object is instantiated.
(Inherited from SerialisableObjectPoolT.)
Public eventObjectReleased
Occurs when an object is released back to the pool.
(Inherited from SerialisableObjectPoolT.)
Top
Remarks

By virtue of being serialisable, SerialisableGenericComponentPoolT can survive an assembly reload caused by live recompilation inside of the Unity editor. However, to ensure Unity is able to serialise fields containing pools, you should subclass SerialisableGenericComponentPoolT by creating a new, non-generic, class derived from it.

SerialisableGenericComponentPoolT achieves this by serialising the instances of the component that were contained in the pool, and then re-adding them to the pool after deserialisation.

To use the SerialisableGenericComponentPoolT, initialise a new instance using the constructor, 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 in this way.

Examples
C#
// Create the pool.
var pool = new SerialisableGenericComponentPool<AudioSource>(parentContainer) {
    InitialSize = 50,
    MaximumSize = 200,
    InstantiateWhenEmpty = true,
    NotificationMode = NotificationMode.Interface,
    LogMessages = LogLevel.Warning
};

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

// Acquire one of the 50 components 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();

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

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