logo SERENITY
Game Systems Foundation
Search Results for

    Show / Hide Table of Contents

    Class UnityCharacterPoolService

    Unity implementation of ICharacterSpawnService for characters. Owns all character pooling, instantiation, and GameObject lifecycle. Uses injected delegates for ViewModel binding to avoid circular assembly dependencies with InterfaceAdapters. Delegates receive Application-layer ICharacterConfig — no Infrastructure types leak into the bind contract. Per architecture Rule 3.3: Character module owns instantiation/pooling.

    Inheritance
    object
    UnityCharacterPoolService
    Implements
    ICharacterSpawnService
    Namespace: Serenity.Character.Infrastructure.Services
    Assembly: Serenity.UnityCharacter.Infrastructure.dll
    Syntax
    public class UnityCharacterPoolService : MonoBehaviour, ICharacterSpawnService

    Constructors

    UnityCharacterPoolService()

    Declaration
    public UnityCharacterPoolService()

    Fields

    _activeById

    Maps CharacterId → PooledCharacter for fast lookup on Release.

    Declaration
    protected readonly Dictionary<string, UnityCharacterPoolService.PooledCharacter> _activeById
    Field Value
    Type Description
    Dictionary<string, UnityCharacterPoolService.PooledCharacter>

    _bind

    Declaration
    protected UnityCharacterPoolService.BindDelegate _bind
    Field Value
    Type Description
    UnityCharacterPoolService.BindDelegate

    _logService

    Declaration
    protected ILogService _logService
    Field Value
    Type Description
    ILogService

    _pool

    Pool buckets keyed by prefab GetInstanceID(). Each bucket holds all instances ever created from that prefab.

    Declaration
    protected readonly Dictionary<int, List<UnityCharacterPoolService.PooledCharacter>> _pool
    Field Value
    Type Description
    Dictionary<int, List<UnityCharacterPoolService.PooledCharacter>>

    _rebind

    Declaration
    protected UnityCharacterPoolService.RebindDelegate _rebind
    Field Value
    Type Description
    UnityCharacterPoolService.RebindDelegate

    _registeredCharacters

    Maps archetype key string → registered ICharacterConfig.

    Declaration
    protected readonly Dictionary<string, ICharacterConfig> _registeredCharacters
    Field Value
    Type Description
    Dictionary<string, ICharacterConfig>

    _unbind

    Declaration
    protected UnityCharacterPoolService.UnbindDelegate _unbind
    Field Value
    Type Description
    UnityCharacterPoolService.UnbindDelegate

    Properties

    ActiveCount

    Declaration
    public int ActiveCount { get; }
    Property Value
    Type Description
    int

    Methods

    GetOrCreateInstance(ICharacterConfig)

    Declaration
    protected UnityCharacterPoolService.PooledCharacter GetOrCreateInstance(ICharacterConfig archetype)
    Parameters
    Type Name Description
    ICharacterConfig archetype
    Returns
    Type Description
    UnityCharacterPoolService.PooledCharacter

    Initialize(ILogService, BindDelegate, RebindDelegate, UnbindDelegate)

    Initializes the pool service with required dependencies. Binding delegates are provided by the installer to avoid circular assembly dependencies between Infrastructure and InterfaceAdapters.

    Declaration
    public void Initialize(ILogService logService, UnityCharacterPoolService.BindDelegate bind, UnityCharacterPoolService.RebindDelegate rebind, UnityCharacterPoolService.UnbindDelegate unbind)
    Parameters
    Type Name Description
    ILogService logService
    UnityCharacterPoolService.BindDelegate bind
    UnityCharacterPoolService.RebindDelegate rebind
    UnityCharacterPoolService.UnbindDelegate unbind

    OnDestroy()

    Declaration
    protected void OnDestroy()

    RegisterCharacters(string, ICharacterConfig)

    Registers a character config so the pool knows which prefab to use for the given character key. Must be called before Spawn for that key.

    Declaration
    public void RegisterCharacters(string archetypeKey, ICharacterConfig config)
    Parameters
    Type Name Description
    string archetypeKey
    ICharacterConfig config

    Release(CharacterId)

    Declaration
    public void Release(CharacterId characterId)
    Parameters
    Type Name Description
    CharacterId characterId

    ReleaseAll()

    Declaration
    public void ReleaseAll()

    ReleaseInstance(PooledCharacter)

    Declaration
    protected void ReleaseInstance(UnityCharacterPoolService.PooledCharacter pooled)
    Parameters
    Type Name Description
    UnityCharacterPoolService.PooledCharacter pooled

    Spawn(string, float, float, float, float, float, float, float)

    Declaration
    public CharacterId Spawn(string archetypeKey, float posX, float posY, float posZ, float rotX, float rotY, float rotZ, float rotW)
    Parameters
    Type Name Description
    string archetypeKey
    float posX
    float posY
    float posZ
    float rotX
    float rotY
    float rotZ
    float rotW
    Returns
    Type Description
    CharacterId

    Implements

    ICharacterSpawnService
    In this article
    © 2026 Serenity. All Rights Reserved