Class SwiftGameObjectPool
- Namespace
- SwiftCollections.Pool
Manages a pool of GameObjects for efficient reuse, reducing memory allocation overhead in Unity.
[Serializable]
public class SwiftGameObjectPool : IDisposable
- Inheritance
-
SwiftGameObjectPool
- Implements
- Inherited Members
Constructors
SwiftGameObjectPool()
Initializes an empty pool for Unity serialization.
public SwiftGameObjectPool()
SwiftGameObjectPool(string, GameObject, int, bool)
Initializes a pool definition.
public SwiftGameObjectPool(string poolName, GameObject prefab, int budget, bool prewarm = false)
Parameters
poolNamestringThe name used to identify the pool.
prefabGameObjectThe prefab instantiated by the pool.
budgetintThe maximum number of live objects.
prewarmboolWhether the owning asset should fill the pool during initialization.
Properties
Budget
Gets the maximum number of live objects managed by the pool.
public int Budget { get; }
Property Value
CreatedObjects
Gets the objects created and tracked by the pool.
public SwiftList<GameObject> CreatedObjects { get; }
Property Value
- SwiftList<GameObject>
PoolName
Gets the name used to identify the pool.
public string PoolName { get; }
Property Value
Prefab
Gets the prefab instantiated by the pool.
public GameObject Prefab { get; }
Property Value
- GameObject
Prewarm
Gets whether the pool should fill its budget during asset initialization.
public bool Prewarm { get; }
Property Value
Methods
Dispose()
Disposes the pool, destroying all managed GameObjects.
public void Dispose()
GetObject(Transform)
Rents an inactive GameObject, reusing an available instance before creating one within the budget.
public GameObject GetObject(Transform parent)
Parameters
parentTransformThe parent transform for the GameObject.
Returns
- GameObject
A pooled GameObject instance.
Exceptions
- ArgumentNullException
parentor Prefab is null.- InvalidOperationException
The budget is invalid or the pool is exhausted.
PrewarmObject(Transform)
Prewarms the pool by instantiating objects up to the budget.
public void PrewarmObject(Transform parent)
Parameters
parentTransformThe parent transform for the prewarmed objects.
Exceptions
- ArgumentNullException
parentor Prefab is null.- InvalidOperationException
The budget is not greater than zero.
ReleaseObject(GameObject, Transform)
Releases a GameObject back into the pool so it can be reused in O(1) time.
public void ReleaseObject(GameObject gameObject, Transform parent)
Parameters
gameObjectGameObjectThe pooled instance to release.
parentTransformThe pool parent transform.
Exceptions
- ArgumentNullException
gameObjectorparentis null.- InvalidOperationException
gameObjectis not checked out from this pool.
TryGetObject(Transform, out GameObject)
Attempts to rent a GameObject from the pool without throwing when the pool is exhausted.
public bool TryGetObject(Transform parent, out GameObject gameObject)
Parameters
parentTransformThe parent transform for the GameObject.
gameObjectGameObjectThe pooled GameObject instance when successful.
Returns
- bool
truewhen an object was acquired; otherwisefalse.
Exceptions
- ArgumentNullException
parentor Prefab is null.- InvalidOperationException
The budget is not greater than zero.