Skip to content

Game.Prefabs.IZoneBuildingComponent

Assembly: Assembly-CSharp
Namespace: Game.Prefabs

Type: interface

Base: (none)

Summary:
Interface used by building prefabs to declare which ECS component types are required for creating building entities (archetypes/prefabs) and to perform per-entity initialization when a building entity is created. Implement this interface in a class that knows which Unity.Entities components a specific building prefab or building level needs and how to initialize their component data. Typical use in Cities: Skylines 2 modding: converting legacy prefab data into DOTS entities or customizing entity composition based on building level or type.


Fields

  • None.
    This interface does not declare any fields. Implementations may keep state in their concrete classes if needed, but the interface itself is stateless.

Properties

  • None.
    There are no properties defined on this interface.

Constructors

  • None.
    Interfaces do not declare constructors. Concrete implementers provide their own constructors if state is required.

Methods

  • void GetBuildingPrefabComponents(HashSet<ComponentType> components, BuildingPrefab buildingPrefab, byte level)
    Adds ComponentType entries to the given HashSet for components that should be present on the building prefab instance for the specified buildingPrefab and level. The set is cumulative: add the component types your implementation requires (do not clear the set). Use ComponentType.ReadWrite() or ComponentType.ReadOnly() to create ComponentType entries. This method is typically called when preparing prefab-level component lists or when assembling the final set of components used by the prefab.

Remarks / best practices: - Only add component types (ComponentType). Do not attempt to set data here. - Keep work minimal and deterministic; this method might be called during prefab registration or archetype construction. - Use the level parameter to include components that are level-dependent.

  • void GetBuildingArchetypeComponents(HashSet<ComponentType> components, BuildingPrefab buildingPrefab, byte level)
    Adds ComponentType entries that are required when creating the ECS Archetype for this building (shared/common components). Conceptually similar to GetBuildingPrefabComponents but focused on archetype-level composition (components that will be part of the entity archetype). Implementers should add all component types that must exist on each entity of this archetype.

Remarks: - Use this to declare components that are consistent for all entities of the archetype. - The distinction between prefab vs archetype methods depends on the host code: prefer Put components required for entity creation in archetype method; use prefab method to include components that vary by prefab instance or require conditional inclusion.

  • void InitializeBuilding(EntityManager entityManager, Entity entity, BuildingPrefab buildingPrefab, byte level)
    Called after a building entity has been created. Use the provided EntityManager to add/set component data, initialize buffers, or perform any other per-entity setup. The buildingPrefab and level parameters allow the implementer to set initial values based on prefab metadata or upgrade level.

Remarks / best practices: - This is the place to call entityManager.AddComponentData, SetComponentData, AddBuffer, etc. - Structural changes (adding/removing components) after archetype creation are possible but may be more expensive — prefer to declare required components in the Get...Components methods when possible. - This method should run on the main thread unless the host explicitly supports calling it from worker threads; assume main-thread safety for EntityManager operations unless documented otherwise.

Usage Example

using System.Collections.Generic;
using Unity.Entities;
using Game.Prefabs;

// Example custom component types (replace with game's real types)
public struct PopulationComponent : IComponentData
{
    public int inhabitants;
}

public struct BuildingRenderComponent : IComponentData
{
    public int lodLevel;
}

// Concrete implementation of IZoneBuildingComponent
public class ResidentialZoneBuildingComponent : IZoneBuildingComponent
{
    public void GetBuildingPrefabComponents(HashSet<ComponentType> components, BuildingPrefab buildingPrefab, byte level)
    {
        // Include render component on the prefab instance
        components.Add(ComponentType.ReadWrite<BuildingRenderComponent>());

        // Optionally include level-dependent components
        if (level > 0)
        {
            components.Add(ComponentType.ReadWrite<PopulationComponent>());
        }
    }

    public void GetBuildingArchetypeComponents(HashSet<ComponentType> components, BuildingPrefab buildingPrefab, byte level)
    {
        // Define archetype-level components common to this building type
        components.Add(ComponentType.ReadWrite<BuildingRenderComponent>());
        components.Add(ComponentType.ReadWrite<PopulationComponent>());
    }

    public void InitializeBuilding(EntityManager entityManager, Entity entity, BuildingPrefab buildingPrefab, byte level)
    {
        // Initialize component data based on level
        var pop = new PopulationComponent { inhabitants = level * 10 };
        entityManager.SetComponentData(entity, pop);

        var render = new BuildingRenderComponent { lodLevel = (level > 2) ? 2 : 1 };
        entityManager.SetComponentData(entity, render);
    }
}

Notes: - Use ComponentType.ReadOnly() when a component will only be read by systems and not written to at initialization time (but still present on the archetype). - BuildingPrefab is a game-specific type containing prefab metadata—use its fields to decide which components and initial data are needed. - Be careful with structural changes at runtime; prefer to declare components up-front in GetBuildingArchetypeComponents / GetBuildingPrefabComponents to avoid costly entity structural changes during initialization.