Skip to content

Game.Prefabs.TrafficSpawner

Assembly:
Assembly-CSharp (Game namespace types compiled into the game's main assembly)

Namespace:
Game.Prefabs

Type:
class

Base:
ComponentBase

Summary:
TrafficSpawner is a prefab component used by building prefabs (and related marker/extension prefabs) to configure in‑game vehicle spawning behaviour for that prefab. It exposes inspector fields to select the road/track type, spawn rate and whether slow vehicles are allowed, and it wires the corresponding ECS components onto the prefab's entity archetype and prefab components during conversion (TrafficSpawnerData, UpdateFrameData, Game.Buildings.TrafficSpawner and optionally ServiceDispatch). Initialize writes the configured values into the entity's TrafficSpawnerData and sets an UpdateFrameData(2) entry so the spawner is updated periodically.


Fields

  • public RoadTypes m_RoadType = RoadTypes.Car
    Used to specify which RoadTypes the spawner will produce vehicles for. Default is RoadTypes.Car. The value is written to the TrafficSpawnerData component during Initialize.

  • public TrackTypes m_TrackType
    Optional/secondary track type (e.g., for rail/tram spawners). Written to TrafficSpawnerData. Defaults to the enum's default value if not set.

  • public float m_SpawnRate = 0.5f
    Rate at which the spawner generates vehicles (configured on the prefab). The value is copied into TrafficSpawnerData.m_SpawnRate in Initialize.

  • public bool m_NoSlowVehicles
    If true, the spawner will avoid creating slow vehicles (e.g., service or special slow-moving units). The flag is stored in TrafficSpawnerData.m_NoSlowVehicles.

Properties

  • None (the component exposes public fields for inspector configuration and overrides ComponentBase methods to add ECS components and initialize them).

Constructors

  • public TrafficSpawner()
    The class uses the default parameterless constructor. Initialization of inspector fields happens via field defaults and the Unity serialization system.

Methods

  • public override void GetPrefabComponents(HashSet<ComponentType> components)
    Adds the prefab-level ECS component types required by this prefab: TrafficSpawnerData and UpdateFrameData. These indicate the entity should include those components when the prefab is converted to an entity.

  • public override void GetArchetypeComponents(HashSet<ComponentType> components)
    Adds runtime archetype components required for instances of this prefab: Game.Buildings.TrafficSpawner (the in-game behavior component). If the prefab does not have a ServiceUpgrade component, this method also adds ServiceDispatch to enable service dispatching behavior for this spawner. This conditional means prefabs with a ServiceUpgrade are treated differently (they likely rely on the upgrade system instead of the generic service dispatch).

  • public override void Initialize(EntityManager entityManager, Entity entity)
    Writes the configured inspector fields to the entity's component data:

  • Sets TrafficSpawnerData { m_SpawnRate, m_RoadType, m_TrackType, m_NoSlowVehicles } from the prefab fields.
  • Sets UpdateFrameData to 2 (registering how frequently this entity should be updated by frame systems).

These overrides are called during prefab→entity conversion so that spawned entities carry the intended configuration.

Usage Example

// Example of what Initialize does — this is from the prefab component itself.
public override void Initialize(EntityManager entityManager, Entity entity)
{
    entityManager.SetComponentData(entity, new TrafficSpawnerData
    {
        m_SpawnRate = m_SpawnRate,
        m_RoadType = m_RoadType,
        m_TrackType = m_TrackType,
        m_NoSlowVehicles = m_NoSlowVehicles
    });
    entityManager.SetComponentData(entity, new UpdateFrameData(2));
}

Additional notes: - To customize spawn behavior for a new prefab, add this component to the prefab and configure m_RoadType/m_TrackType/m_SpawnRate/m_NoSlowVehicles in the inspector. During conversion, the ECS components will be attached and initialized automatically. - The Game.Buildings.TrafficSpawner and TrafficSpawnerData types are the runtime representations used by the simulation systems to actually spawn and manage vehicles; ensure those systems are present/compatible when creating custom spawners.