Game.Prefabs.ServiceConsumption
Assembly: Assembly-CSharp (typical Unity / Cities: Skylines 2 runtime assembly)
Namespace: Game.Prefabs
Type: Class
Base: ComponentBase, IServiceUpgrade
Summary:
ServiceConsumption is a prefab component used by building prefabs to define their runtime service needs and upkeep. It exposes simple serialized fields (upkeep, electricity, water, garbage, telecom need) and converts them into the ECS ConsumptionData component that is attached to the Entity representing the building. The class ensures the ConsumptionData component is included in the prefab components / archetype (unless an upgrade prefab replaces it) and writes the configured values into the entity during initialization.
Fields
-
public int m_Upkeep
Used to populate ConsumptionData.m_Upkeep. Represents the monetary upkeep cost (configured on the prefab) that will be written to the entity. -
public int m_ElectricityConsumption
Used to populate ConsumptionData.m_ElectricityConsumption. Represents the building's electricity demand value as configured on the prefab. -
public int m_WaterConsumption
Used to populate ConsumptionData.m_WaterConsumption. Represents the building's water demand value as configured on the prefab. -
public int m_GarbageAccumulation
Used to populate ConsumptionData.m_GarbageAccumulation. Represents how much garbage the building accumulates (per whatever game interval the simulation uses) and is written into ConsumptionData. -
public float m_TelecomNeed
Used to populate ConsumptionData.m_TelecomNeed. A float representing telecom / network need for the building.
Properties
- None. (This component exposes only public fields; it does not declare CLR properties.)
Constructors
public ServiceConsumption()
Default parameterless constructor. The component relies on its serialized fields being set in the prefab (inspector or prefab authoring code) and does not perform additional construction logic.
Methods
-
public override void GetPrefabComponents(HashSet<ComponentType> components)
Adds the ConsumptionData component type (ReadWrite) to the set of components that the prefab will include. This ensures the ECS entity created for the prefab will have a ConsumptionData component. -
public override void GetArchetypeComponents(HashSet<ComponentType> components)
If the base prefab does not have a ServiceUpgrade component, this method adds archetype components required by the ConsumptionData (via GetConsumptionData().AddArchetypeComponents). This controls which component types are present on entities created from this prefab. -
public void GetUpgradeComponents(HashSet<ComponentType> components)
Called when building upgrades are processed (implements IServiceUpgrade). Adds ConsumptionData's archetype components for the upgraded variant so upgraded entities will also get the correct ECS components. -
public override void Initialize(EntityManager entityManager, Entity entity)
Called when the prefab is instantiated into an ECS entity. This method constructs a ConsumptionData instance from the prefab fields and writes it into the entity using entityManager.SetComponentData(entity, ConsumptionData). -
private ConsumptionData GetConsumptionData()
Helper method that creates and returns a ConsumptionData struct populated from the prefab's serialized fields (m_Upkeep, m_ElectricityConsumption, m_WaterConsumption, m_GarbageAccumulation, m_TelecomNeed). Used by GetArchetypeComponents, GetUpgradeComponents, and Initialize.
Usage Example
// After a prefab with ServiceConsumption is instantiated into an Entity,
// read back the data from the entity to inspect the configured values.
var consumption = entityManager.GetComponentData<ConsumptionData>(entity);
UnityEngine.Debug.Log($"Upkeep: {consumption.m_Upkeep}, Electricity: {consumption.m_ElectricityConsumption}, Water: {consumption.m_WaterConsumption}");
Additional notes: - If a prefab has an associated ServiceUpgrade component, the base prefab defers archetype component addition to the upgrade mechanism (so upgraded prefabs can supply their own ConsumptionData). - The numeric interpretation (units and update interval) of these fields follows the game's simulation conventions — the component merely transfers configured values into the ECS ConsumptionData structure.