CharacterSpawner
Scene-placed MonoBehaviour for spawning networked characters. Supports both Inspector-configured spawning (via FactorableSpawner) and static spawning by character ID (via CharacterDatabase).
Definition
Namespace: Paragon.Townskeep.CharacterSystem
Assembly: Townskeep.dll
public class CharacterSpawner : SerializedMonoBehaviourInheritance: SerializedMonoBehaviour → CharacterSpawner
Remarks
CharacterSpawner provides two spawning patterns:
Instance methods — Uses an Inspector-configured
FactorableSpawner<Character, CharacterData>to spawn a specific character type at the spawner'stransform.position. The factory and variant are selected in the Inspector.Static methods — Spawns any character by
characterIDusingCharacterDatabase.GetFactory(). Does not require a scene instance.
All spawning goes through NetworkScriptableFactory.NetworkSpawn(), creating replicated NetworkObject instances via Unity Netcode.
The Spawn() button is available in the Inspector during Play Mode via [Button, ShowIf("@UnityEngine.Application.isPlaying")].
Quick Lookup
Spawn from Inspector-configured factory
spawner.Spawn(ownerID, predicted)
Spawn and wait for network ready
await spawner.SpawnAsync(ownerID, predicted)
Spawn any character by ID (static)
CharacterSpawner.Spawn(characterID, variantID, ownerID, predicted)
Spawn any character async (instance)
await spawner.SpawnAsync(characterID, variantID, ownerID, predicted)
Methods
Spawn (instance)
Spawns a character using the Inspector-configured FactorableSpawner, positioned at the spawner's transform.
ownerID
ulong?
Network owner client ID, or null for server-owned
predicted
bool
Whether to use client-side prediction
Returns: The spawned Character instance.
SpawnAsync (instance, configured factory)
Spawns via the configured factory and awaits until the NetworkObject.IsSpawned is true.
Spawn (static, by character ID)
Spawns any character by looking up its factory from CharacterDatabase. Uses variant index 0.
characterID
int
The CharacterID to look up in CharacterDatabase
ownerID
ulong?
Network owner client ID
predicted
bool
Client-side prediction flag
Spawn (static, by character ID + variant)
Spawns any character with a specific variant.
characterID
int
The CharacterID to look up
variantID
int
Data variant index (0 = source data)
ownerID
ulong?
Network owner client ID
predicted
bool
Client-side prediction flag
SpawnAsync (instance, by character ID)
Async spawn by character ID. Awaits NetworkObject.IsSpawned.
Usage Example
Common Pitfalls
Instance Spawn sets position, static Spawn does not — The instance Spawn() method positions the character at transform.position and calls Physics.SyncTransforms(). The static Spawn() methods do not set any position — the character spawns at the prefab's default position.
SpawnAsync uses busy-wait — SpawnAsync polls NetworkObject.IsSpawned via Yield.WaitWhile(). On slow networks, this may take multiple frames.
Static Spawn requires valid CharacterDatabase — The static methods call CharacterDatabase.GetFactory(), which throws KeyNotFoundException for invalid character IDs.
See Also
CharacterFactory — the factory used for actual instantiation
CharacterDatabase — provides factory lookup by character ID for static methods
FactorableSpawner — the wrapped spawner used by instance methods
Last updated