Class ScyllaObjectPool<T>.Builder
Fluent builder for constructing ScyllaObjectPool instances with complex configurations. Supports prefab, capacity, max size, creation function, callbacks (onGet, onRelease, onDestroy), collection checking, automatic creation, and synchronized wrapper creation.
Inherited Members
Namespace: Scylla.Core.Structures
Assembly: ScyllaCore.dll
Syntax
public sealed class ScyllaObjectPool<T>.Builder
Remarks
The builder provides a discoverable API for configuring Unity object pools without relying on positional parameters. Use Build() to obtain a configured pool (returns interface for synchronized wrappers). Use BuildConcrete() to obtain the concrete unsynchronized pool type (throws if synchronized).
// Basic usage with prefab
var pool = ScyllaObjectPool<GameObject>.CreateBuilder()
.WithPrefab(myPrefab)
.Build();
// Configure capacity and max size
var pool = ScyllaObjectPool<GameObject>.CreateBuilder()
.WithPrefab(myPrefab)
.WithCapacity(64)
.WithMaxSize(200)
.Build();
// Custom creation function and callbacks
var pool = ScyllaObjectPool<GameObject>.CreateBuilder()
.WithCreateFunc(() => Instantiate(myPrefab))
.OnGet(obj => obj.SetActive(true))
.OnRelease(obj => obj.SetActive(false))
.Build();
// Create synchronized (thread-safe) wrapper
var pool = ScyllaObjectPool<GameObject>.CreateBuilder()
.WithPrefab(myPrefab)
.WithCapacity(128)
.Synchronized()
.Build();
Methods
AllowCreate(bool)
Enables automatic creation of new instances when the pool is empty. When enabled, Get() and TryGet(out T) create new instances when the pool is empty. When disabled, TryGet(out T) returns false when empty.
Declaration
public ScyllaObjectPool<T>.Builder AllowCreate(bool value = true)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | value | True to enable automatic creation, false to disable (default: true) |
Returns
| Type | Description |
|---|---|
| ScyllaObjectPool<T>.Builder | This builder instance for method chaining |
Build()
Builds and returns the configured ScyllaObjectPool instance or synchronized wrapper. If Synchronized(object) was called, returns a ScyllaObjectPool<T>.SynchronizedScyllaObjectPool wrapper. Otherwise, returns the unsynchronized ScyllaObjectPool<T>.
Declaration
public IScyllaCollection<T> Build()
Returns
| Type | Description |
|---|---|
| IScyllaCollection<T> | An IScyllaCollection<T> representing either the unsynchronized pool or synchronized wrapper |
BuildConcrete()
Builds and returns the concrete unsynchronized ScyllaObjectPool<T> instance. This method provides access to the concrete type instead of the interface.
Declaration
public ScyllaObjectPool<T> BuildConcrete()
Returns
| Type | Description |
|---|---|
| ScyllaObjectPool<T> | The configured unsynchronized ScyllaObjectPool instance |
Exceptions
| Type | Condition |
|---|---|
| InvalidOperationException | Thrown when Synchronized(object) was called. Use Build() to obtain the synchronized wrapper. |
CollectionCheck(bool)
Enables collection checking, which validates that instances are not released to the pool twice. When enabled, Release(T) checks whether the instance is already in the pool (O(n)). Useful for debugging; disable for maximum performance.
Declaration
public ScyllaObjectPool<T>.Builder CollectionCheck(bool value = true)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | value | True to enable collection checking, false to disable (default: true) |
Returns
| Type | Description |
|---|---|
| ScyllaObjectPool<T>.Builder | This builder instance for method chaining |
OnDestroy(Action<T>)
Sets a callback invoked before an instance is destroyed (e.g., when pool is full). Useful for custom cleanup logic when Unity objects are removed from the pool.
Declaration
public ScyllaObjectPool<T>.Builder OnDestroy(Action<T> onDestroy)
Parameters
| Type | Name | Description |
|---|---|---|
| Action<T> | onDestroy | The callback to invoke, or null to disable |
Returns
| Type | Description |
|---|---|
| ScyllaObjectPool<T>.Builder | This builder instance for method chaining |
OnGet(Action<T>)
Sets a callback invoked after an instance is retrieved from the pool.
Useful for activating Unity objects (e.g., SetActive(true) for GameObjects).
Declaration
public ScyllaObjectPool<T>.Builder OnGet(Action<T> onGet)
Parameters
| Type | Name | Description |
|---|---|---|
| Action<T> | onGet | The callback to invoke, or null to disable |
Returns
| Type | Description |
|---|---|
| ScyllaObjectPool<T>.Builder | This builder instance for method chaining |
OnRelease(Action<T>)
Sets a callback invoked before an instance is stored back into the pool.
Useful for deactivating Unity objects (e.g., SetActive(false) for GameObjects).
Declaration
public ScyllaObjectPool<T>.Builder OnRelease(Action<T> onRelease)
Parameters
| Type | Name | Description |
|---|---|---|
| Action<T> | onRelease | The callback to invoke, or null to disable |
Returns
| Type | Description |
|---|---|
| ScyllaObjectPool<T>.Builder | This builder instance for method chaining |
Synchronized(object)
Configures the builder to return a synchronized (thread-safe) wrapper around the pool. All operations on the synchronized wrapper will be protected by a lock.
Declaration
public ScyllaObjectPool<T>.Builder Synchronized(object syncRoot = null)
Parameters
| Type | Name | Description |
|---|---|---|
| object | syncRoot | Optional external lock object to use for synchronization. If null, the wrapper creates and owns a private lock. Provide a custom sync root to coordinate synchronization across multiple synchronized collections. |
Returns
| Type | Description |
|---|---|
| ScyllaObjectPool<T>.Builder | This builder instance for method chaining |
WithCapacity(int)
Sets the initial capacity of the free-list buffer.
Declaration
public ScyllaObjectPool<T>.Builder WithCapacity(int capacity)
Parameters
| Type | Name | Description |
|---|---|---|
| int | capacity | The initial capacity. Must be at least 1. If less than 1, will be clamped to 1 during construction. |
Returns
| Type | Description |
|---|---|
| ScyllaObjectPool<T>.Builder | This builder instance for method chaining |
Exceptions
| Type | Condition |
|---|---|
| ArgumentOutOfRangeException | Thrown when capacity is less than 1 |
WithCreateFunc(Func<T>)
Sets a custom creation function used when the pool is empty and automatic creation is enabled. If not set and automatic creation is enabled, creation falls back to instantiating the prefab (if provided).
Declaration
public ScyllaObjectPool<T>.Builder WithCreateFunc(Func<T> createFunc)
Parameters
| Type | Name | Description |
|---|---|---|
| Func<T> | createFunc | The function to create new Unity object instances, or null to use prefab instantiation. |
Returns
| Type | Description |
|---|---|
| ScyllaObjectPool<T>.Builder | This builder instance for method chaining |
WithMaxSize(int)
Sets the maximum number of inactive instances that can be stored in the pool. When exceeded, released instances are destroyed instead of being stored.
Declaration
public ScyllaObjectPool<T>.Builder WithMaxSize(int maxSize)
Parameters
| Type | Name | Description |
|---|---|---|
| int | maxSize | The maximum size. Must be at least 0. If negative, will be clamped to 0 during construction. Default is MaxValue (unlimited). |
Returns
| Type | Description |
|---|---|
| ScyllaObjectPool<T>.Builder | This builder instance for method chaining |
Exceptions
| Type | Condition |
|---|---|
| ArgumentOutOfRangeException | Thrown when maxSize is negative |
WithPrefab(T)
Sets the Unity prefab to instantiate when the pool is empty and automatic creation is enabled. If not set, you must provide WithCreateFunc(Func<T>) when automatic creation is enabled.
Declaration
public ScyllaObjectPool<T>.Builder WithPrefab(T prefab)
Parameters
| Type | Name | Description |
|---|---|---|
| T | prefab | The Unity prefab to instantiate, or null to disable prefab-based creation. |
Returns
| Type | Description |
|---|---|
| ScyllaObjectPool<T>.Builder | This builder instance for method chaining |