Table of Contents

Class SwiftGameObjectPool

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

poolName string

The name used to identify the pool.

prefab GameObject

The prefab instantiated by the pool.

budget int

The maximum number of live objects.

prewarm bool

Whether 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

int

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

string

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

bool

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

parent Transform

The parent transform for the GameObject.

Returns

GameObject

A pooled GameObject instance.

Exceptions

ArgumentNullException

parent or 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

parent Transform

The parent transform for the prewarmed objects.

Exceptions

ArgumentNullException

parent or 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

gameObject GameObject

The pooled instance to release.

parent Transform

The pool parent transform.

Exceptions

ArgumentNullException

gameObject or parent is null.

InvalidOperationException

gameObject is 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

parent Transform

The parent transform for the GameObject.

gameObject GameObject

The pooled GameObject instance when successful.

Returns

bool

true when an object was acquired; otherwise false.

Exceptions

ArgumentNullException

parent or Prefab is null.

InvalidOperationException

The budget is not greater than zero.