Skip to content

Game.Prefabs.MultipleUnitTrainFrontPrefab

Assembly:
Assembly-CSharp

Namespace:
Game.Prefabs

Type:
class

Base:
TrainPrefab

Summary:
Prefab class used to define the front (leading) unit of a multiple-unit train. Exposes configuration for minimum/maximum multiple-unit counts, a list of carriage prefabs with per-carriage min/max counts and direction, and an option to automatically add a reversed end carriage. During initialization it writes train engine data and populates the VehicleCarriageElement buffer with entity references for each configured carriage (and optionally a reversed end carriage pointing back to this front prefab).


Fields

  • public int m_MinMultipleUnitCount = 1
    Minimum number of multiple units allowed for this train front. Written into the TrainEngineData component during LateInitialize.

  • public int m_MaxMultipleUnitCount = 1
    Maximum number of multiple units allowed for this train front. Written into the TrainEngineData component during LateInitialize.

  • public MultipleUnitTrainCarriageInfo[] m_Carriages
    Array of carriage configuration entries. Each MultipleUnitTrainCarriageInfo contains a reference to a carriage prefab and per-carriage settings (min/max count and direction). During LateInitialize each entry is resolved to an Entity (via PrefabSystem) and added to the VehicleCarriageElement dynamic buffer.

  • public bool m_AddReversedEndCarriage = true
    If true, an additional VehicleCarriageElement is appended referencing this front prefab as a reversed end carriage (1..1, VehicleCarriageDirection.Reversed).

Properties

  • (none declared on this class)
    This prefab does not declare its own properties beyond the public fields. Component data is created/set in LateInitialize.

Constructors

  • public MultipleUnitTrainFrontPrefab()
    Default constructor (implicit). Initialization is typically performed by the prefab system and LateInitialize rather than in the constructor.

Methods

  • public override void GetDependencies(List<PrefabBase> prefabs)
    Adds any referenced carriage prefabs from m_Carriages to the provided prefabs list so the prefab system knows about dependencies. Calls base.GetDependencies(prefabs) first.

  • public override void GetPrefabComponents(HashSet<ComponentType> components)
    Adds required ECS component types for this prefab:

  • TrainEngineData (ReadWrite)
  • MultipleUnitTrainData (ReadWrite)
  • VehicleCarriageElement (ReadWrite)
    Also calls base.GetPrefabComponents(components).

  • public override void LateInitialize(EntityManager entityManager, Entity entity)
    Finalizes prefab-to-entity setup:

  • Calls base.LateInitialize(entityManager, entity).
  • Writes a TrainEngineData component on the entity using m_MinMultipleUnitCount and m_MaxMultipleUnitCount.
  • Obtains the DynamicBuffer for the entity and, for each entry in m_Carriages:
    • Resolves the referenced prefab to an Entity via PrefabSystem.GetEntity(...)
    • Adds a VehicleCarriageElement to the buffer with the resolved entity, per-carriage min/max, and direction.
  • If m_AddReversedEndCarriage is true, appends a VehicleCarriageElement referencing this front prefab entity with count 1..1 and VehicleCarriageDirection.Reversed.

Notes and behavior details: - PrefabSystem is retrieved with entityManager.World.GetExistingSystemManaged() to resolve carriage prefab->Entity mapping. - m_Carriages can be null; code checks for null before iterating. - The method assumes the entity already has a VehicleCarriageElement buffer component (GetPrefabComponents adds that), and that the PrefabSystem knows about referenced carriages.

Usage Example

// Example: configure the prefab in code (typically done via inspector in Unity)
var frontPrefab = new MultipleUnitTrainFrontPrefab();
frontPrefab.m_MinMultipleUnitCount = 2;
frontPrefab.m_MaxMultipleUnitCount = 4;
// frontPrefab.m_Carriages = ... assign MultipleUnitTrainCarriageInfo[] via inspector or code
frontPrefab.m_AddReversedEndCarriage = true;

// At runtime the prefab system will call LateInitialize when converting the prefab to an ECS entity.
// LateInitialize will create TrainEngineData and populate the VehicleCarriageElement buffer.