Runtime Spawner 1.0.0
Generic Runtime spawn and instance pooling system for spawning random AI agents around a map. Works with ANY AI system easily.
Loading...
Searching...
No Matches
MegaCrush.Spawner.RuntimeSpawner Class Reference

A generic object spawning system, supports local regions for density controls. More...

Inheritance diagram for MegaCrush.Spawner.RuntimeSpawner:

Public Member Functions

void Init ()
 Initialize all of the spawners, find child regions and clear all cache's.
 
void StartSpawners ()
 The primary Entry point - start this spawner!
 
void SetPlayer (GameObject player)
 Set the active player for the spawner. Useful if you spawn your player object dynamically at runtime.
 
void StartWaveSpawner (string spawnerName)
 
void SetStartAutomatically (bool value)
 should this spawner start automatically when the scene starts?
 
void AddRegion ()
 Editor only - add a new region.
 
void ShowHideSpawnVolumes ()
 Editor only - toggle show / hide spawn regions in scene view.
 
void ShowHideWaveTriggers ()
 
WaveTrigger AddWaveTrigger ()
 
void ShowOccluderRange ()
 Editor only, shows scene view gizmo.
 
void DrawWorldExtents ()
 Editor only, shows scene view gizmo.
 

Static Public Member Functions

static WaveSpawner CreateWaveSpawner (string path)
 
static SpawnEntry CreateSpawnEntry (string path)
 

Properties

bool UseInternalCulling [get, set]
 Should we do culling ourselves?
 
bool UseGlobalSpawners [get, set]
 Do we have global spawners? otherwise only region spawners will be active.
 
bool UseRegionSpawners [get, set]
 Does this spawner have custom region spawners?
 
bool UseWaveSpawners [get, set]
 Does this spawner have wave spawners?
 
string PlayerTag [get, set]
 
List< GameObject > SpawnedObjects [get]
 The list of currently spawned objects.
 
List< SpawnEntryGlobalSpawners [get, set]
 List of global spawners.
 
List< string > NavmeshAreas [get, set]
 
List< LocalAreaSpawnerSpawnRegions [get, set]
 Our list of spawn regions.
 
List< WaveTriggerWaveTriggers [get, set]
 Wave triggers.
 
float CullDistance [get]
 Range that objects will be culled if we are doing internal culling.
 
Vector2 WorldSizeVector2 [get]
 Get the specified world size as a Vector2.
 
float MinSpawnRange [get]
 Min spawn range.
 
int MinGlobalObjectCount [get, set]
 min global object count
 
int MaxGlobalObjectCount [get, set]
 max global object count
 
Vector3 WorldSizeVector3 [get]
 Returns the world size as a Vector3 (y is set to 5 arbitrarily)
 
bool LogSpawnEntries [get]
 Used by the inspector, editor only.
 
bool DrawExtents [get]
 Editor only, draws the world size.
 

Events

static Action< GameObject > onObjectSpawned
 Listen to this in your AI objects so they know that they were spawned.
 
static Action< GameObject > onObjectCulled
 An object was culled.
 
static Action< int > onUpdateCurrentPopulation
 Population was changed.
 
static Action< float > onSpawnerInit
 When this spawner was initialized.
 
static Action< WaveEntryonNewWaveTriggered
 A wave was triggered.
 
static Action< WaveTriggeronWaveTriggerActivated
 
static Action< WaveTriggeronWaveSpawnerComplete
 

Private Member Functions

void Awake ()
 
void OnDisable ()
 
void Start ()
 
void CreateObjectPools ()
 Create Object Pools for all of the spawners used by this spawner.
 
IEnumerator DoCulling ()
 Our internal culling routine.
 
IEnumerator StartGlobalSpawners ()
 Runs the global spawner loop.
 
IEnumerator StartRegionSpawners ()
 Run our spawn loop.
 
void OnPlayerEnteredRegion (LocalAreaSpawner region, bool isInRegion)
 Event handler for when the player enters or leaves a spawn region.
 
IEnumerator StartWaveSpawners (WaveTrigger thisTrigger)
 Wave spawners are handled slightly differently. Instead of just starting, they might be activated by a trigger or the API. Once activated we will iterate through the waves as defined in the WaveSpawner scriptable object.
 
void UpdatePopulation ()
 Internally whenever we spawn new objects we add them into the newWaveSpawners list. This method iterates through the list and called the onObjectSpawned event to let other scripts know that they objects were spawned.
 
void OnWaveTriggerActivated (WaveTrigger thisTrigger)
 Event handler from the WaveTrigger to start spawning.
 
void OnUnitDied (GameObject thisObject)
 Event callback - an object died.
 
void OnObjectCulled (GameObject thisObject)
 callback if the object in question gets too far from the player
 
GameObject SpawnObject (SpawnEntry thisEntry, LocalAreaSpawner thisRegion=null, WaveTrigger trigger=null, Transform spawnPoint=null, float spawnRange=0f, bool isGlobal=false, GameObject groupParent=null)
 Spawn an object based on the given configuration.
 
Vector3 GetSpawnPosition (GameObject instance, SpawnEntry entry, LocalAreaSpawner region=null, WaveTrigger trigger=null, Transform spawnPoint=null, float spawnRange=1f, bool isGlobal=false, GameObject parentObject=null)
 Get a random spawn position. This is the meat of the whole system.
 
RuntimeSpawnerSettings GetSettings ()
 
void OnDrawGizmosSelected ()
 

Static Private Member Functions

static void CreateNewSpawner ()
 Add a menu option to create a new spawner.
 

Private Attributes

string playerTag = "Player"
 
bool autoLaunchOnStart = false
 Should the spawner start automatically?
 
bool logSpawnEntries
 log spawn entries to the console
 
GameObject playerObject
 The player.
 
Vector2 worldSize = new Vector2(50, 50)
 Size of the global spawn region.
 
bool drawExtents = false
 Editor only: draw extents of worldSize in scene view.
 
float minSpawnRange
 don't spawn objects within this range of the player
 
bool useInternalCulling = true
 Use our internal culling system to remove distant spawned objects.
 
float cullDistance
 How far away do they need to be before culling them?
 
int cullingCheckFrequency = 2
 How often we should check for objects to be culled.
 
string spawnerTag = string.Empty
 Tag to apply to objects that have been spawned.
 
bool useGlobalSpawners = true
 Use global spawners?
 
List< SpawnEntryglobalSpawners = new()
 Our list of global spawners.
 
LayerMask layerMask
 Only spawn agents on the specified layers, otherwise spawned anywhere.
 
List< string > restrictToNavmeshAreas = new()
 Only spawn global objects on these navmesh areas.
 
int minGlobalObjectCount = 25
 Minimum number of global objects we should spawn.
 
int maxGlobalObjectCount = 25
 Maximum number of global objects we should spawn.
 
int maxObjectCount = 100
 Max overall population we should have active at one time.
 
bool useRegionSpawners = true
 Are we using region spawners?
 
List< LocalAreaSpawnerspawnRegions = new()
 
List< LocalAreaSpawneractiveSpawnRegions = new()
 keep track of any regions that the player is in
 
bool useWaveSpawners = true
 
List< WaveTriggerwaveTriggers = new()
 List of wave triggers.
 
List< GameObject > spawnedObjects = new()
 all of the spawned objects
 
List< GameObject > newGlobalSpawners = new()
 global objects that were spawned the current spawn turn
 
List< GameObject > newRegionSpawners = new()
 global objects that were spawned the current spawn turn
 
List< GameObject > newWaveSpawners = new()
 global objects that were spawned the current spawn turn
 
Dictionary< string, int > spawnerCache = new()
 Cache the names of spawner objects so we can track their population.
 
int currentPopulation = 0
 The current global population of this spawner.
 
NavMeshHit navmeshHit
 
RaycastHit raycastHit
 
bool showSpawnVolumes = true
 
bool showSpawnTriggers = false
 
bool showOccluderRange = false
 
int navmeshAreaMask = 0
 
bool initialized = false
 
RuntimeSpawnerSettings settings
 

Detailed Description

A generic object spawning system, supports local regions for density controls.

Member Function Documentation

◆ AddRegion()

void MegaCrush.Spawner.RuntimeSpawner.AddRegion ( )

Editor only - add a new region.

◆ AddWaveTrigger()

WaveTrigger MegaCrush.Spawner.RuntimeSpawner.AddWaveTrigger ( )

◆ Awake()

void MegaCrush.Spawner.RuntimeSpawner.Awake ( )
private

◆ CreateNewSpawner()

static void MegaCrush.Spawner.RuntimeSpawner.CreateNewSpawner ( )
staticprivate

Add a menu option to create a new spawner.

◆ CreateObjectPools()

void MegaCrush.Spawner.RuntimeSpawner.CreateObjectPools ( )
private

Create Object Pools for all of the spawners used by this spawner.

◆ CreateSpawnEntry()

static SpawnEntry MegaCrush.Spawner.RuntimeSpawner.CreateSpawnEntry ( string path)
static

◆ CreateWaveSpawner()

static WaveSpawner MegaCrush.Spawner.RuntimeSpawner.CreateWaveSpawner ( string path)
static

◆ DoCulling()

IEnumerator MegaCrush.Spawner.RuntimeSpawner.DoCulling ( )
private

Our internal culling routine.

Returns

◆ DrawWorldExtents()

void MegaCrush.Spawner.RuntimeSpawner.DrawWorldExtents ( )

Editor only, shows scene view gizmo.

◆ GetSettings()

RuntimeSpawnerSettings MegaCrush.Spawner.RuntimeSpawner.GetSettings ( )
private

◆ GetSpawnPosition()

Vector3 MegaCrush.Spawner.RuntimeSpawner.GetSpawnPosition ( GameObject instance,
SpawnEntry entry,
LocalAreaSpawner region = null,
WaveTrigger trigger = null,
Transform spawnPoint = null,
float spawnRange = 1f,
bool isGlobal = false,
GameObject parentObject = null )
private

Get a random spawn position. This is the meat of the whole system.

Parameters
instancethe object we are positioning
entryspawn config for the object
spawnerthe local area spawner (if one)
spawnPointwave spawner spawnpoint position (if one)
rangeoffset range for the wave spawner spawnpoint (if one)
isGlobalis this a global spawner?
parentObjectif this is a group spawner, then this is the first in the group
Returns

◆ Init()

void MegaCrush.Spawner.RuntimeSpawner.Init ( )

Initialize all of the spawners, find child regions and clear all cache's.

Note: called automatically by StartSpawners()

◆ OnDisable()

void MegaCrush.Spawner.RuntimeSpawner.OnDisable ( )
private

◆ OnDrawGizmosSelected()

void MegaCrush.Spawner.RuntimeSpawner.OnDrawGizmosSelected ( )
private

◆ OnObjectCulled()

void MegaCrush.Spawner.RuntimeSpawner.OnObjectCulled ( GameObject thisObject)
private

callback if the object in question gets too far from the player

Parameters
thisObject

◆ OnPlayerEnteredRegion()

void MegaCrush.Spawner.RuntimeSpawner.OnPlayerEnteredRegion ( LocalAreaSpawner region,
bool isInRegion )
private

Event handler for when the player enters or leaves a spawn region.

Parameters
regionThe region the player is entering / leaving
isInRegiontrue if entered, false if leaving

◆ OnUnitDied()

void MegaCrush.Spawner.RuntimeSpawner.OnUnitDied ( GameObject thisObject)
private

Event callback - an object died.

Parameters
thisObject

◆ OnWaveTriggerActivated()

void MegaCrush.Spawner.RuntimeSpawner.OnWaveTriggerActivated ( WaveTrigger thisTrigger)
private

Event handler from the WaveTrigger to start spawning.

Parameters
thisTrigger

◆ SetPlayer()

void MegaCrush.Spawner.RuntimeSpawner.SetPlayer ( GameObject player)

Set the active player for the spawner. Useful if you spawn your player object dynamically at runtime.

Parameters
playerThe GameObject for the current player

◆ SetStartAutomatically()

void MegaCrush.Spawner.RuntimeSpawner.SetStartAutomatically ( bool value)

should this spawner start automatically when the scene starts?

Parameters
valuetrue or false

◆ ShowHideSpawnVolumes()

void MegaCrush.Spawner.RuntimeSpawner.ShowHideSpawnVolumes ( )

Editor only - toggle show / hide spawn regions in scene view.

◆ ShowHideWaveTriggers()

void MegaCrush.Spawner.RuntimeSpawner.ShowHideWaveTriggers ( )

◆ ShowOccluderRange()

void MegaCrush.Spawner.RuntimeSpawner.ShowOccluderRange ( )

Editor only, shows scene view gizmo.

◆ SpawnObject()

GameObject MegaCrush.Spawner.RuntimeSpawner.SpawnObject ( SpawnEntry thisEntry,
LocalAreaSpawner thisRegion = null,
WaveTrigger trigger = null,
Transform spawnPoint = null,
float spawnRange = 0f,
bool isGlobal = false,
GameObject groupParent = null )
private

Spawn an object based on the given configuration.

Parameters
thisEntrythe object config
thisRegiona local region we are spawning in
isGlobalis this a global spawn?
groupParentis it a group spawn - this is the first / parent object
Returns

◆ Start()

void MegaCrush.Spawner.RuntimeSpawner.Start ( )
private

◆ StartGlobalSpawners()

IEnumerator MegaCrush.Spawner.RuntimeSpawner.StartGlobalSpawners ( )
private

Runs the global spawner loop.

◆ StartRegionSpawners()

IEnumerator MegaCrush.Spawner.RuntimeSpawner.StartRegionSpawners ( )
private

Run our spawn loop.

Returns

◆ StartSpawners()

void MegaCrush.Spawner.RuntimeSpawner.StartSpawners ( )

The primary Entry point - start this spawner!

◆ StartWaveSpawner()

void MegaCrush.Spawner.RuntimeSpawner.StartWaveSpawner ( string spawnerName)

◆ StartWaveSpawners()

IEnumerator MegaCrush.Spawner.RuntimeSpawner.StartWaveSpawners ( WaveTrigger thisTrigger)
private

Wave spawners are handled slightly differently. Instead of just starting, they might be activated by a trigger or the API. Once activated we will iterate through the waves as defined in the WaveSpawner scriptable object.

Returns

◆ UpdatePopulation()

void MegaCrush.Spawner.RuntimeSpawner.UpdatePopulation ( )
private

Internally whenever we spawn new objects we add them into the newWaveSpawners list. This method iterates through the list and called the onObjectSpawned event to let other scripts know that they objects were spawned.

Member Data Documentation

◆ activeSpawnRegions

List<LocalAreaSpawner> MegaCrush.Spawner.RuntimeSpawner.activeSpawnRegions = new()
private

keep track of any regions that the player is in

◆ autoLaunchOnStart

bool MegaCrush.Spawner.RuntimeSpawner.autoLaunchOnStart = false
private

Should the spawner start automatically?

◆ cullDistance

float MegaCrush.Spawner.RuntimeSpawner.cullDistance
private

How far away do they need to be before culling them?

◆ cullingCheckFrequency

int MegaCrush.Spawner.RuntimeSpawner.cullingCheckFrequency = 2
private

How often we should check for objects to be culled.

◆ currentPopulation

int MegaCrush.Spawner.RuntimeSpawner.currentPopulation = 0
private

The current global population of this spawner.

◆ drawExtents

bool MegaCrush.Spawner.RuntimeSpawner.drawExtents = false
private

Editor only: draw extents of worldSize in scene view.

◆ globalSpawners

List<SpawnEntry> MegaCrush.Spawner.RuntimeSpawner.globalSpawners = new()
private

Our list of global spawners.

◆ initialized

bool MegaCrush.Spawner.RuntimeSpawner.initialized = false
private

◆ layerMask

LayerMask MegaCrush.Spawner.RuntimeSpawner.layerMask
private

Only spawn agents on the specified layers, otherwise spawned anywhere.

◆ logSpawnEntries

bool MegaCrush.Spawner.RuntimeSpawner.logSpawnEntries
private

log spawn entries to the console

◆ maxGlobalObjectCount

int MegaCrush.Spawner.RuntimeSpawner.maxGlobalObjectCount = 25
private

Maximum number of global objects we should spawn.

◆ maxObjectCount

int MegaCrush.Spawner.RuntimeSpawner.maxObjectCount = 100
private

Max overall population we should have active at one time.

◆ minGlobalObjectCount

int MegaCrush.Spawner.RuntimeSpawner.minGlobalObjectCount = 25
private

Minimum number of global objects we should spawn.

◆ minSpawnRange

float MegaCrush.Spawner.RuntimeSpawner.minSpawnRange
private

don't spawn objects within this range of the player

◆ navmeshAreaMask

int MegaCrush.Spawner.RuntimeSpawner.navmeshAreaMask = 0
private

◆ navmeshHit

NavMeshHit MegaCrush.Spawner.RuntimeSpawner.navmeshHit
private

◆ newGlobalSpawners

List<GameObject> MegaCrush.Spawner.RuntimeSpawner.newGlobalSpawners = new()
private

global objects that were spawned the current spawn turn

◆ newRegionSpawners

List<GameObject> MegaCrush.Spawner.RuntimeSpawner.newRegionSpawners = new()
private

global objects that were spawned the current spawn turn

◆ newWaveSpawners

List<GameObject> MegaCrush.Spawner.RuntimeSpawner.newWaveSpawners = new()
private

global objects that were spawned the current spawn turn

◆ playerObject

GameObject MegaCrush.Spawner.RuntimeSpawner.playerObject
private

The player.

◆ playerTag

string MegaCrush.Spawner.RuntimeSpawner.playerTag = "Player"
private

◆ raycastHit

RaycastHit MegaCrush.Spawner.RuntimeSpawner.raycastHit
private

◆ restrictToNavmeshAreas

List<string> MegaCrush.Spawner.RuntimeSpawner.restrictToNavmeshAreas = new()
private

Only spawn global objects on these navmesh areas.

◆ settings

RuntimeSpawnerSettings MegaCrush.Spawner.RuntimeSpawner.settings
private

◆ showOccluderRange

bool MegaCrush.Spawner.RuntimeSpawner.showOccluderRange = false
private

◆ showSpawnTriggers

bool MegaCrush.Spawner.RuntimeSpawner.showSpawnTriggers = false
private

◆ showSpawnVolumes

bool MegaCrush.Spawner.RuntimeSpawner.showSpawnVolumes = true
private

◆ spawnedObjects

List<GameObject> MegaCrush.Spawner.RuntimeSpawner.spawnedObjects = new()
private

all of the spawned objects

◆ spawnerCache

Dictionary<string, int> MegaCrush.Spawner.RuntimeSpawner.spawnerCache = new()
private

Cache the names of spawner objects so we can track their population.

◆ spawnerTag

string MegaCrush.Spawner.RuntimeSpawner.spawnerTag = string.Empty
private

Tag to apply to objects that have been spawned.

◆ spawnRegions

List<LocalAreaSpawner> MegaCrush.Spawner.RuntimeSpawner.spawnRegions = new()
private

◆ useGlobalSpawners

bool MegaCrush.Spawner.RuntimeSpawner.useGlobalSpawners = true
private

Use global spawners?

◆ useInternalCulling

bool MegaCrush.Spawner.RuntimeSpawner.useInternalCulling = true
private

Use our internal culling system to remove distant spawned objects.

◆ useRegionSpawners

bool MegaCrush.Spawner.RuntimeSpawner.useRegionSpawners = true
private

Are we using region spawners?

◆ useWaveSpawners

bool MegaCrush.Spawner.RuntimeSpawner.useWaveSpawners = true
private

◆ waveTriggers

List<WaveTrigger> MegaCrush.Spawner.RuntimeSpawner.waveTriggers = new()
private

List of wave triggers.

◆ worldSize

Vector2 MegaCrush.Spawner.RuntimeSpawner.worldSize = new Vector2(50, 50)
private

Size of the global spawn region.

Property Documentation

◆ CullDistance

float MegaCrush.Spawner.RuntimeSpawner.CullDistance
get

Range that objects will be culled if we are doing internal culling.

◆ DrawExtents

bool MegaCrush.Spawner.RuntimeSpawner.DrawExtents
get

Editor only, draws the world size.

◆ GlobalSpawners

List<SpawnEntry> MegaCrush.Spawner.RuntimeSpawner.GlobalSpawners
getset

List of global spawners.

◆ LogSpawnEntries

bool MegaCrush.Spawner.RuntimeSpawner.LogSpawnEntries
get

Used by the inspector, editor only.

◆ MaxGlobalObjectCount

int MegaCrush.Spawner.RuntimeSpawner.MaxGlobalObjectCount
getset

max global object count

◆ MinGlobalObjectCount

int MegaCrush.Spawner.RuntimeSpawner.MinGlobalObjectCount
getset

min global object count

◆ MinSpawnRange

float MegaCrush.Spawner.RuntimeSpawner.MinSpawnRange
get

Min spawn range.

◆ NavmeshAreas

List<string> MegaCrush.Spawner.RuntimeSpawner.NavmeshAreas
getset

◆ PlayerTag

string MegaCrush.Spawner.RuntimeSpawner.PlayerTag
getset

◆ SpawnedObjects

List<GameObject> MegaCrush.Spawner.RuntimeSpawner.SpawnedObjects
get

The list of currently spawned objects.

◆ SpawnRegions

List<LocalAreaSpawner> MegaCrush.Spawner.RuntimeSpawner.SpawnRegions
getset

Our list of spawn regions.

◆ UseGlobalSpawners

bool MegaCrush.Spawner.RuntimeSpawner.UseGlobalSpawners
getset

Do we have global spawners? otherwise only region spawners will be active.

◆ UseInternalCulling

bool MegaCrush.Spawner.RuntimeSpawner.UseInternalCulling
getset

Should we do culling ourselves?

◆ UseRegionSpawners

bool MegaCrush.Spawner.RuntimeSpawner.UseRegionSpawners
getset

Does this spawner have custom region spawners?

◆ UseWaveSpawners

bool MegaCrush.Spawner.RuntimeSpawner.UseWaveSpawners
getset

Does this spawner have wave spawners?

◆ WaveTriggers

List<WaveTrigger> MegaCrush.Spawner.RuntimeSpawner.WaveTriggers
getset

Wave triggers.

◆ WorldSizeVector2

Vector2 MegaCrush.Spawner.RuntimeSpawner.WorldSizeVector2
get

Get the specified world size as a Vector2.

◆ WorldSizeVector3

Vector3 MegaCrush.Spawner.RuntimeSpawner.WorldSizeVector3
get

Returns the world size as a Vector3 (y is set to 5 arbitrarily)

Event Documentation

◆ onNewWaveTriggered

Action<WaveEntry> MegaCrush.Spawner.RuntimeSpawner.onNewWaveTriggered
static

A wave was triggered.

◆ onObjectCulled

Action<GameObject> MegaCrush.Spawner.RuntimeSpawner.onObjectCulled
static

An object was culled.

◆ onObjectSpawned

Action<GameObject> MegaCrush.Spawner.RuntimeSpawner.onObjectSpawned
static

Listen to this in your AI objects so they know that they were spawned.

◆ onSpawnerInit

Action<float> MegaCrush.Spawner.RuntimeSpawner.onSpawnerInit
static

When this spawner was initialized.

◆ onUpdateCurrentPopulation

Action<int> MegaCrush.Spawner.RuntimeSpawner.onUpdateCurrentPopulation
static

Population was changed.

◆ onWaveSpawnerComplete

Action<WaveTrigger> MegaCrush.Spawner.RuntimeSpawner.onWaveSpawnerComplete
static

◆ onWaveTriggerActivated

Action<WaveTrigger> MegaCrush.Spawner.RuntimeSpawner.onWaveTriggerActivated
static

The documentation for this class was generated from the following file: