![]() |
Runtime Spawner 1.3.0
Generic Runtime spawn and instance pooling system for spawning random AI agents around a map. Works with ANY AI system easily.
|
Central coordinator for runtime spawning. Supports three spawn flows:
Also provides optional internal culling beyond a configurable distance and integrates with an object pool. More...
Classes | |
struct | DeferredJob |
Internal record for deferred placement attempts when placement requires NavMesh but none is present yet. More... | |
Public Member Functions | |
void | SetConcurrentAliveCap (int cap) |
Sets the upper limit for concurrently alive spawned objects (global/region/wave combined). | |
void | SetGlobalSpawnWindow (int min, int max) |
Sets the target global spawn window. The global loop tries to keep population within these bounds. | |
void | SetSpawnRateMultiplier (float mult) |
Applies a multiplier to the global spawn tick rate. Values < 1 slow down, > 1 speed up. | |
void | SetWaveRateMultiplier (float mult) |
Applies a multiplier to the wave interval timing. Values < 1 shorten intervals, > 1 lengthen them. | |
void | ForceDeferredTick () |
Optional nudge (e.g., from a baker) to force the deferred loop to tick immediately. | |
void | Init () |
Discovers child regions, wires services, synchronizes events, and initializes collaborators. Safe to call multiple times; subsequent calls re-scan regions and clear internal caches. | |
void | StartSpawners () |
Creates object pools and starts the enabled spawn loops (global, region, wave collaborators). Calls Init if not already initialized. | |
void | SetPlayer (GameObject player) |
Assigns the player object used for distance checks and context building. | |
void | SetStartAutomatically (bool value) |
Sets whether the spawner should automatically initialize and start on Start. | |
bool | RegisterRegion (LocalAreaSpawner region) |
bool | UnregisterRegion (LocalAreaSpawner region) |
bool | RegisterWaveTrigger (WaveTrigger trigger) |
bool | UnregisterWaveTrigger (WaveTrigger trigger) |
void | ActivateWaveTrigger (WaveTrigger trigger) |
Programmatically activates a wave trigger using the same internal flow as volume activation. | |
void | DeactivateWaveTrigger (WaveTrigger trigger) |
Stops a running wave sequence and marks the trigger as inactive. | |
void | NotifyWaveTriggerActivated (WaveTrigger trigger) |
Notifies listeners that a wave trigger has been activated. Safe instance-side way to forward triggers without touching static events. | |
void | AddRegion () |
Creates a new child LocalAreaSpawner with a default BoxCollider trigger. | |
void | ShowHideSpawnVolumes () |
Toggles visibility of region spawn volumes in Scene View. | |
void | ShowHideWaveTriggers () |
Toggles visibility of wave triggers in Scene View. | |
WaveTrigger | AddWaveTrigger () |
Creates and adds a new WaveTrigger child with a default BoxCollider trigger. | |
void | ShowOccluderRange () |
Toggles drawing of the culling radius gizmo around the player. | |
void | DrawWorldExtents () |
Toggles drawing of world extents gizmo. | |
Static Public Member Functions | |
static void | Despawn (GameObject go) |
Convenience API to despawn a spawned object when the owner spawner is unknown. Attempts to resolve the owner spawner; falls back to any spawner in scene or destroys the object. | |
static WaveSpawner | CreateWaveSpawner (string path) |
Creates a WaveSpawner asset at the given project path. | |
static SpawnEntry | CreateSpawnEntry (string path) |
Creates a SpawnEntry asset at the given project path. | |
Properties | |
bool | UseInternalCulling [get, set] |
Enables the built-in culling loop that despawns objects farther than cullDistance from the player. | |
bool | UseGlobalSpawners [get, set] |
Enables global spawners (non-localized spawns that obey global population caps). | |
bool | UseRegionSpawners [get, set] |
Enables region spawners (localized spawns activated by player presence in LocalAreaSpawners). | |
bool | UseWaveSpawners [get, set] |
Enables wave spawners (scriptable wave tables triggered via WaveTrigger). | |
string | PlayerTag [get, set] |
Tag used to locate the player (if playerObject is not assigned). | |
List< GameObject > | SpawnedObjects [get] |
Live list of spawned objects. Kept in sync by PopulationTracker. | |
List< SpawnEntry > | GlobalSpawners [get, set] |
Global spawn entries used by the global spawn loop. | |
List< string > | NavmeshAreas [get, set] |
Optional list of NavMesh area names to constrain spawn placement. | |
List< LocalAreaSpawner > | SpawnRegions [get, set] |
All configured spawn regions (children discovered on Init). | |
List< WaveTrigger > | WaveTriggers [get, set] |
Configured wave triggers for table-driven encounters. | |
float | CullDistance [get] |
Culling radius in world units. | |
Vector2 | WorldSizeVector2 [get] |
World extents (X,Z) for global spawning and gizmos. | |
float | MinSpawnRange [get] |
Minimum allowed distance from player for spawn placement. | |
int | MinGlobalObjectCount [get, set] |
Minimum desired global population (the global loop tries to maintain between min/max). | |
int | MaxGlobalObjectCount [get, set] |
Maximum desired global population (the global loop tries to maintain between min/max). | |
Transform | PlayerTransform [get] |
Player transform for range checks and spawn context; returns null if unknown. | |
Vector3 | WorldSizeVector3 [get] |
World extents as a 3D vector (Y fixed to 5 for visualization). | |
bool | LogSpawnEntries [get] |
Enables logging of key spawn events to the Console. | |
bool | DrawExtents [get] |
When true, draws world extents in Scene View. | |
bool | IsInitialized [get] |
property so other services can check if the spawner is active and ready | |
bool | UseSpawnHints [get, set] |
SpawnHintSettings | HintSettings [get, set] |
PopulationTracker | Population [get] |
Exposes the population tracker. | |
ISpawnExecutor | Executor [get] |
Exposes the spawn executor used by all loops. | |
ISpawnLocator | Locator [get] |
Exposes the spawn locator used for placement queries. | |
Events | |
static Action< GameObject > | onObjectSpawned |
Raised when an object is spawned by any loop. | |
static Action< GameObject > | onObjectCulled |
Raised when an object is culled (internal or manual despawn). | |
static Action< int > | onUpdateCurrentPopulation |
Raised when total spawned population changes. | |
static Action< float > | onSpawnerInit |
Raised after Init; supplies the configured cullDistance. | |
static Action< WaveEntry > | onNewWaveTriggered |
Raised when a wave entry is about to begin. | |
static Action< WaveTrigger > | onWaveTriggerActivated |
Raised when a wave trigger volume becomes active. | |
static Action< WaveTrigger > | onWaveSpawnerComplete |
Raised when a wave spawner completes its sequence. | |
Private Member Functions | |
IEnumerator | ProcessDeferredPlacements () |
Coroutine that periodically re-attempts placement for deferred jobs. Uses exponential backoff per job. | |
void | Awake () |
void | OnDisable () |
void | Start () |
void | CreateObjectPools () |
Ensures object pools exist for all configured global, region, and wave entries. | |
IEnumerator | DoCulling () |
Periodically checks spawned objects and despawns them if they exceed CullDistance. | |
void | DespawnInternal (GameObject go) |
Returns an instance to the pool, updates population and events, and clears ownership bookkeeping. | |
void | OnPlayerEnteredRegion (LocalAreaSpawner region, bool isInRegion) |
Maintains the active region set based on player presence notifications from LocalAreaSpawner. | |
void | OnWaveTriggerActivated (WaveTrigger thisTrigger) |
Handles activation of wave triggers and starts their wave sequence coroutine. | |
IEnumerator | DeferWaveUntilInitialized (WaveTrigger trigger) |
IEnumerator | RunWaveSequence (WaveTrigger trigger) |
Runs the configured waves for a trigger once (per-wave looping policy honored inside the runner). | |
void | OnUnitDied (GameObject thisObject) |
Callback hook for unit death; returns the object to pool and updates bookkeeping. | |
void | OnObjectCulled (GameObject thisObject) |
Callback hook for culling; returns the object to pool and updates bookkeeping. | |
RuntimeSpawnerSettings | GetSettings () |
Loads or creates the editor settings asset for gizmo colors and other preferences. | |
void | OnDrawGizmosSelected () |
Static Private Attributes | |
static readonly Dictionary< GameObject, RuntimeSpawner > | s_OwnerByGO = new() |
Static registry mapping spawned instances back to their owning RuntimeSpawner. Used to route Despawn(GameObject) when the owner isn't directly known. | |
Central coordinator for runtime spawning. Supports three spawn flows:
Also provides optional internal culling beyond a configurable distance and integrates with an object pool.
void MegaCrush.Spawner.RuntimeSpawner.ActivateWaveTrigger | ( | WaveTrigger | trigger | ) |
Programmatically activates a wave trigger using the same internal flow as volume activation.
void MegaCrush.Spawner.RuntimeSpawner.AddRegion | ( | ) |
Creates a new child LocalAreaSpawner with a default BoxCollider trigger.
WaveTrigger MegaCrush.Spawner.RuntimeSpawner.AddWaveTrigger | ( | ) |
Creates and adds a new WaveTrigger child with a default BoxCollider trigger.
|
private |
|
private |
Ensures object pools exist for all configured global, region, and wave entries.
|
static |
Creates a SpawnEntry asset at the given project path.
|
static |
Creates a WaveSpawner asset at the given project path.
void MegaCrush.Spawner.RuntimeSpawner.DeactivateWaveTrigger | ( | WaveTrigger | trigger | ) |
Stops a running wave sequence and marks the trigger as inactive.
|
private |
|
static |
Convenience API to despawn a spawned object when the owner spawner is unknown. Attempts to resolve the owner spawner; falls back to any spawner in scene or destroys the object.
go | Spawned object to despawn. |
|
private |
Returns an instance to the pool, updates population and events, and clears ownership bookkeeping.
go | Object to despawn. |
|
private |
Periodically checks spawned objects and despawns them if they exceed CullDistance.
void MegaCrush.Spawner.RuntimeSpawner.DrawWorldExtents | ( | ) |
Toggles drawing of world extents gizmo.
void MegaCrush.Spawner.RuntimeSpawner.ForceDeferredTick | ( | ) |
Optional nudge (e.g., from a baker) to force the deferred loop to tick immediately.
|
private |
Loads or creates the editor settings asset for gizmo colors and other preferences.
void MegaCrush.Spawner.RuntimeSpawner.Init | ( | ) |
Discovers child regions, wires services, synchronizes events, and initializes collaborators. Safe to call multiple times; subsequent calls re-scan regions and clear internal caches.
void MegaCrush.Spawner.RuntimeSpawner.NotifyWaveTriggerActivated | ( | WaveTrigger | trigger | ) |
Notifies listeners that a wave trigger has been activated. Safe instance-side way to forward triggers without touching static events.
|
private |
|
private |
|
private |
Callback hook for culling; returns the object to pool and updates bookkeeping.
|
private |
Maintains the active region set based on player presence notifications from LocalAreaSpawner.
|
private |
Callback hook for unit death; returns the object to pool and updates bookkeeping.
|
private |
Handles activation of wave triggers and starts their wave sequence coroutine.
|
private |
Coroutine that periodically re-attempts placement for deferred jobs. Uses exponential backoff per job.
bool MegaCrush.Spawner.RuntimeSpawner.RegisterRegion | ( | LocalAreaSpawner | region | ) |
bool MegaCrush.Spawner.RuntimeSpawner.RegisterWaveTrigger | ( | WaveTrigger | trigger | ) |
|
private |
Runs the configured waves for a trigger once (per-wave looping policy honored inside the runner).
void MegaCrush.Spawner.RuntimeSpawner.SetConcurrentAliveCap | ( | int | cap | ) |
Sets the upper limit for concurrently alive spawned objects (global/region/wave combined).
cap | New concurrent alive cap (min 1). |
void MegaCrush.Spawner.RuntimeSpawner.SetGlobalSpawnWindow | ( | int | min, |
int | max ) |
Sets the target global spawn window. The global loop tries to keep population within these bounds.
min | Minimum desired population (clamped to ≥ 0). |
max | Maximum desired population (clamped to ≥ min ). |
void MegaCrush.Spawner.RuntimeSpawner.SetPlayer | ( | GameObject | player | ) |
Assigns the player object used for distance checks and context building.
void MegaCrush.Spawner.RuntimeSpawner.SetSpawnRateMultiplier | ( | float | mult | ) |
Applies a multiplier to the global spawn tick rate. Values < 1 slow down, > 1 speed up.
mult | Rate multiplier (min 0.01). |
void MegaCrush.Spawner.RuntimeSpawner.SetStartAutomatically | ( | bool | value | ) |
Sets whether the spawner should automatically initialize and start on Start.
void MegaCrush.Spawner.RuntimeSpawner.SetWaveRateMultiplier | ( | float | mult | ) |
Applies a multiplier to the wave interval timing. Values < 1 shorten intervals, > 1 lengthen them.
mult | Wave timing multiplier (min 0.01). |
void MegaCrush.Spawner.RuntimeSpawner.ShowHideSpawnVolumes | ( | ) |
Toggles visibility of region spawn volumes in Scene View.
void MegaCrush.Spawner.RuntimeSpawner.ShowHideWaveTriggers | ( | ) |
Toggles visibility of wave triggers in Scene View.
void MegaCrush.Spawner.RuntimeSpawner.ShowOccluderRange | ( | ) |
Toggles drawing of the culling radius gizmo around the player.
|
private |
void MegaCrush.Spawner.RuntimeSpawner.StartSpawners | ( | ) |
Creates object pools and starts the enabled spawn loops (global, region, wave collaborators). Calls Init if not already initialized.
bool MegaCrush.Spawner.RuntimeSpawner.UnregisterRegion | ( | LocalAreaSpawner | region | ) |
bool MegaCrush.Spawner.RuntimeSpawner.UnregisterWaveTrigger | ( | WaveTrigger | trigger | ) |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
staticprivate |
Static registry mapping spawned instances back to their owning RuntimeSpawner. Used to route Despawn(GameObject) when the owner isn't directly known.
|
private |
|
private |
|
private |
|
private |
|
private |
Retained for Inspector display; kept in sync by PopulationTracker.
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
get |
Culling radius in world units.
|
get |
When true, draws world extents in Scene View.
|
get |
Exposes the spawn executor used by all loops.
|
getset |
Global spawn entries used by the global spawn loop.
|
getset |
|
get |
property so other services can check if the spawner is active and ready
|
get |
Exposes the spawn locator used for placement queries.
|
get |
Enables logging of key spawn events to the Console.
|
getset |
Maximum desired global population (the global loop tries to maintain between min/max).
|
getset |
Minimum desired global population (the global loop tries to maintain between min/max).
|
get |
Minimum allowed distance from player for spawn placement.
|
getset |
Optional list of NavMesh area names to constrain spawn placement.
|
getset |
Tag used to locate the player (if playerObject is not assigned).
|
get |
Player transform for range checks and spawn context; returns null
if unknown.
|
get |
Exposes the population tracker.
|
get |
Live list of spawned objects. Kept in sync by PopulationTracker.
|
getset |
All configured spawn regions (children discovered on Init).
|
getset |
Enables global spawners (non-localized spawns that obey global population caps).
|
getset |
Enables the built-in culling loop that despawns objects farther than cullDistance from the player.
|
getset |
Enables region spawners (localized spawns activated by player presence in LocalAreaSpawners).
|
getset |
|
getset |
Enables wave spawners (scriptable wave tables triggered via WaveTrigger).
|
getset |
Configured wave triggers for table-driven encounters.
|
get |
World extents (X,Z) for global spawning and gizmos.
|
get |
World extents as a 3D vector (Y fixed to 5 for visualization).
|
static |
Raised when a wave entry is about to begin.
|
static |
Raised when an object is culled (internal or manual despawn).
|
static |
Raised when an object is spawned by any loop.
|
static |
Raised after Init; supplies the configured cullDistance.
|
static |
Raised when total spawned population changes.
|
static |
Raised when a wave spawner completes its sequence.
|
static |
Raised when a wave trigger volume becomes active.