Skip to content

Game.Prefabs.RoutePrefab

Assembly:
Game (assembly containing the game's prefab types)

Namespace:
Game.Prefabs

Type:
class

Base:
PrefabBase, implements IColored

Summary:
RoutePrefab is a prefab component that describes route visuals and the ECS archetypes used to spawn route-related entities (routes, waypoints, connected waypoints and segments). It exposes material, width, segment length and color used for rendering routes, and it sets up the necessary Entity archetypes in LateInitialize so the runtime can create runtime route entities efficiently. This prefab is intended for use by the route system in Cities: Skylines 2 and by mods that add or customize route content.


Fields

  • public Material m_Material
    Material used for rendering the route (mesh/material assigned to route segments).

  • public float m_Width = 4f
    Visual width of the rendered route segments. Default value: 4.0.

  • public float m_SegmentLength = 64f
    Target length of a single route segment. Controls how route geometry is split into segments for rendering/processing. Default value: 64.0.

  • public UnityEngine.Color m_Color = UnityEngine.Color.magenta
    Base color for the route. Serialized so it can be set from the inspector. Default: magenta.

  • public string m_LocaleID
    Optional locale identifier (string) used for localization of route names or UI text associated with the prefab.

Properties

  • public Color32 color { get; private set }
    Returns the prefab color as a Color32 converted from m_Color. Implements IColored.color. Useful where the color must be consumed in Color32 form (e.g., shader/mesh color buffers).

Constructors

  • public RoutePrefab()
    Default parameterless constructor. Field initializers set default width, segment length and color. Typical creation is via Unity prefab creation/assignment; no special construction logic is required.

Methods

  • public override void GetPrefabComponents(HashSet<ComponentType> components)
    Registers high-level prefab components required by this prefab. Implementation calls base.GetPrefabComponents and adds ReadWrite() to signal that the prefab entity will carry RouteData. Modders that add custom runtime route data should call base and then add their ComponentType entries.

  • public override void GetArchetypeComponents(HashSet<ComponentType> components)
    Contributes to archetype composition when building archetypes that incorporate this prefab. Behavior:

  • If the archetype includes Route, the method adds RouteWaypoint, RouteSegment, Game.Routes.Color, and RouteBufferIndex components to that archetype.
  • Else if the archetype includes Waypoint, it adds Position and Owner.
  • Else if the archetype includes Segment, it adds CurveElement and Owner. This allows the prefab system to compute full archetypes for routes/waypoints/segments automatically. If you extend route entities with additional component types, ensure your components' GetArchetypeComponents are invoked so their requirements are merged.

  • public override void LateInitialize(EntityManager entityManager, Entity entity)
    Called late in prefab initialization to create and store concrete EntityArchetypes used by the route system. Implementation details:

  • Gathers component requirements from contained ComponentBase instances.
  • Builds four archetype hashsets for route, waypoint, connected-waypoint, and segment entities.
  • Adds runtime tags such as Created and Updated for lifecycle tracking.
  • Uses entityManager.CreateArchetype to create the archetypes and stores them into the RouteData component on the prefab entity (m_RouteArchetype, m_WaypointArchetype, m_ConnectedArchetype, m_SegmentArchetype). This method is crucial: it converts the prefab authoring-time setup into concrete ECS archetypes used at runtime to spawn entities efficiently. If you change components or add custom route-related components, LateInitialize will pick them up so spawned entities have the required components.

Usage Example

Example: configure a RoutePrefab in code (e.g., editor script) and read the archetypes after LateInitialize:

// Create/configure a RoutePrefab on a GameObject (Editor or runtime setup)
var routePrefab = someGameObject.AddComponent<RoutePrefab>();
routePrefab.m_Material = myRouteMaterial;
routePrefab.m_Width = 6f;
routePrefab.m_SegmentLength = 128f;
routePrefab.m_Color = Color.cyan;
routePrefab.m_LocaleID = "ROUTE_MAIN";

// After the prefab system runs LateInitialize, you can access the generated archetypes:
// (Assumes you have the prefab entity reference; typically provided by the prefab system)
EntityManager em = World.DefaultGameObjectInjectionWorld.EntityManager;
RouteData routeData = em.GetComponentData<RouteData>(prefabEntity);

// Use routeData.m_RouteArchetype, m_WaypointArchetype, etc. to create route entities:
Entity routeEntity = em.CreateEntity(routeData.m_RouteArchetype);
// ... then set initial components/data on routeEntity as required.

Notes and modding tips: - If you add new route-related components, ensure their GetArchetypeComponents implementations participate so LateInitialize includes them in the computed archetypes. - Archetype creation is done once per prefab entity and cached in RouteData to avoid repeated costs. Changing the prefab after initialization may require re-running LateInitialize or rebuilding the prefab entity. - The prefab only defines archetype composition and visual defaults; actual spawning logic (placement of waypoints, creation of segments, rendering) is performed by route systems that consume these archetypes and the prefab data.