MF_StaticAutoPool
The main interface for using the object pools. In all cases, object pools are referenced by the prefab they contain.
Example usage from anywhere in the project:
MF_AutoPool.Spawn( prefab );
MF_AutoPool.Despawn( object );
prefab is a reference to the prefab to spawn, and object is a reference to a spawned object.
Public Class MF_AutoPool
Public Static Methods
InitializeSpawn : bool ( prefab : GameObject )
Initializes the pool containing prefab and can add items to that pool's size.
This method is optional, and is used to ensure all pool reference links are created at the time this method is called. With heavy pool usage, this may reduce lag spikes by creating all references early in the scene.
InitializeSpawn : bool ( prefab : GameObject, addPool : float, minPool : int )
InitializeSpawn : bool ( prefab : GameObject, addPool : float, minPool : int, emptyBehavior AP_enum.EmptyBehavior, maxEmptyBehavior : AP_enum.MaxEmptyBehavior )
Additionally, this method can also be used to dynamically construct pools at runtime.
If the pool manager object AP_Manager in the scene is marked allowCreate and allowModify as true: Each time this method is called a pool will be constructed with a minimum of minPool objects. If minPool has already been reached, it will add addPool objects. If minPool was not 0, then the pool size will be increased to minPool + addPool items. If addPool is a float < 1, addPool will be that percentage of items already in the pool.
emptyBehavior and maxEmptyBehavior set the respective properties of the pool being created or modified.
If the pool manager allowCreate is false, pools cannot be created by InitializeSpawn().
If the pool manager allowModify is false, pools cannot be modified by InitializeSpawn(), however InitializeSpawn() is always allowed to modify a pool it is in the process of creating.
Spawn : GameObject ( prefab : GameObject )
Spawn : GameObject ( prefab : GameObject, child : int? )
Spawn : GameObject ( prefab : GameObject, pos : Vector3, rot : Quaternion )
Spawn : GameObject ( prefab : GameObject, child : int?, pos : Vector3, rot : Quaternion )
Spawns an object from the pool containing prefab at pos position and rot rotation.
if pos and rot are unused, the position and rotation will be that of the pool object.
Optionally, a child index can be specified for prefabs containing children. That child will be enabled, and the others will remain disabled. This feature can be used to simulate different types of objects.
Despawn : bool ( obj : GameObject )
Despawn : bool ( obj : GameObject, time : float )
Deactivates obj but waits the delay on its AP_Reference script before returning to the pool.
If time is specified and > 0, time will be used instead.
Returns false if the item could not be despawned.
These Despawn() methods are slightly slower than the methods below, because these must use obj.GetComponent<AP_Reference>() to find the script, whereas the ones below reference it directly.
Despawn : bool ( script : AP_Reference )
Despawn : bool ( script : AP_Reference, time : float )
Deactivates the object attached to script but waits the delay on AP_Reference script before returning to the pool.
If time is specified and > 0, time will be used instead.
Returns false if the item could not be despawned.
These methods are slightly faster than using the above Despawn() methods with a GameObject as a parameter.
GetActiveCount : int ( prefab : GameObject )
Returns the number of active spawns in the scene from the pool containing prefab.
GetAvailableCount : int ( prefab : GameObject )
Returns the number of unspawned items from the pool containing prefab.
DespawnPool : bool ( prefab : GameObject )
Despawn all objects belonging to the pool containing prefab.
Will return false if prefab is null or the pool couldn't be found.
DespawnAll : bool ( )
Despawn all objects belonging to all pools, including currently active spawns.
Will return false if prefab is null or one of the pools couldn't be found.
RemovePool : bool ( prefab : GameObject )
Destroy the pool containing prefab and all its items, including currently active spawns.
Will return false if prefab is null or the pool couldn't be found.
RemoveAll : bool ( )
Destroy all pools and all their items, including currently active spawns. The pool manager will remain intact.
Will return false if one of the pools couldn't be found.
The main interface for using the object pools. In all cases, object pools are referenced by the prefab they contain.
Example usage from anywhere in the project:
MF_AutoPool.Spawn( prefab );
MF_AutoPool.Despawn( object );
prefab is a reference to the prefab to spawn, and object is a reference to a spawned object.
Public Class MF_AutoPool
Public Static Methods
InitializeSpawn : bool ( prefab : GameObject )
Initializes the pool containing prefab and can add items to that pool's size.
This method is optional, and is used to ensure all pool reference links are created at the time this method is called. With heavy pool usage, this may reduce lag spikes by creating all references early in the scene.
InitializeSpawn : bool ( prefab : GameObject, addPool : float, minPool : int )
InitializeSpawn : bool ( prefab : GameObject, addPool : float, minPool : int, emptyBehavior AP_enum.EmptyBehavior, maxEmptyBehavior : AP_enum.MaxEmptyBehavior )
Additionally, this method can also be used to dynamically construct pools at runtime.
If the pool manager object AP_Manager in the scene is marked allowCreate and allowModify as true: Each time this method is called a pool will be constructed with a minimum of minPool objects. If minPool has already been reached, it will add addPool objects. If minPool was not 0, then the pool size will be increased to minPool + addPool items. If addPool is a float < 1, addPool will be that percentage of items already in the pool.
emptyBehavior and maxEmptyBehavior set the respective properties of the pool being created or modified.
If the pool manager allowCreate is false, pools cannot be created by InitializeSpawn().
If the pool manager allowModify is false, pools cannot be modified by InitializeSpawn(), however InitializeSpawn() is always allowed to modify a pool it is in the process of creating.
Spawn : GameObject ( prefab : GameObject )
Spawn : GameObject ( prefab : GameObject, child : int? )
Spawn : GameObject ( prefab : GameObject, pos : Vector3, rot : Quaternion )
Spawn : GameObject ( prefab : GameObject, child : int?, pos : Vector3, rot : Quaternion )
Spawns an object from the pool containing prefab at pos position and rot rotation.
if pos and rot are unused, the position and rotation will be that of the pool object.
Optionally, a child index can be specified for prefabs containing children. That child will be enabled, and the others will remain disabled. This feature can be used to simulate different types of objects.
Despawn : bool ( obj : GameObject )
Despawn : bool ( obj : GameObject, time : float )
Deactivates obj but waits the delay on its AP_Reference script before returning to the pool.
If time is specified and > 0, time will be used instead.
Returns false if the item could not be despawned.
These Despawn() methods are slightly slower than the methods below, because these must use obj.GetComponent<AP_Reference>() to find the script, whereas the ones below reference it directly.
Despawn : bool ( script : AP_Reference )
Despawn : bool ( script : AP_Reference, time : float )
Deactivates the object attached to script but waits the delay on AP_Reference script before returning to the pool.
If time is specified and > 0, time will be used instead.
Returns false if the item could not be despawned.
These methods are slightly faster than using the above Despawn() methods with a GameObject as a parameter.
GetActiveCount : int ( prefab : GameObject )
Returns the number of active spawns in the scene from the pool containing prefab.
GetAvailableCount : int ( prefab : GameObject )
Returns the number of unspawned items from the pool containing prefab.
DespawnPool : bool ( prefab : GameObject )
Despawn all objects belonging to the pool containing prefab.
Will return false if prefab is null or the pool couldn't be found.
DespawnAll : bool ( )
Despawn all objects belonging to all pools, including currently active spawns.
Will return false if prefab is null or one of the pools couldn't be found.
RemovePool : bool ( prefab : GameObject )
Destroy the pool containing prefab and all its items, including currently active spawns.
Will return false if prefab is null or the pool couldn't be found.
RemoveAll : bool ( )
Destroy all pools and all their items, including currently active spawns. The pool manager will remain intact.
Will return false if one of the pools couldn't be found.