Constructor Pool
- Namespace
- Gamelogic.Extensions
- Assembly
- Gamelogic.Extensions.dll
Pool(int, Func<T>, Action<T>, Action<T>, Action<T>)
Initializes a new instance of the Pool<T> class.
public Pool(int initialCapacity, Func<T> create, Action<T> destroy, Action<T> activate, Action<T> deactivate)
Parameters
initialCapacity
intThe initial capacity of the pool, that is, how many object to create.
create
Func<T>A function that creates a new object of type
T
. This should create objects in the active state; they will be deactivated by the pool. This function should (ideally) also instantiate everything the class will need, and do any tasks that do not be repeated each time the object is made active.destroy
Action<T>The action that destroys an object of type
T
. If it is an ordinary C# object, you do not need to provide a destroy method — references to the object from this object will be removed, and (provided you do not have references to it somewhere too), it will be released naturally like any C# object. IfT
is a Unity Object, or a Component whose UnityEngine.Component.gameObject you want to be destroyed, this action should do that. Also consider using the MonoBehaviourPool<T> class instead. This action is called by methods that allow you to specify whether to deactivate objects first, so this action need not do any tasks that are already done bydeactivate
action.activate
Action<T>A function called when an object is activated, when it is acquired.
deactivate
Action<T>A function called when an object is activated, either when releasing it, or when it is created for the first time. If your object accumulate references to other objects while it is active, and they cannot be re-used, you should remove them in this action, so that these objects do not hog memory while they are inactive.