Skip to content

Game.Prefabs.ResourceProducer

Assembly:
Assembly-CSharp (game/mod assembly)

Namespace:
Game.Prefabs

Type:
class

Base:
ComponentBase, IServiceUpgrade

Summary:
Component used on building prefabs to configure resource production for CityService buildings. When the prefab is converted to an ECS entity this component: - exposes a DynamicBuffer on the entity (populated from m_Resources), - adds runtime archetype components (Game.Economy.Resources and Game.Buildings.ResourceProducer) for non-upgrade variants, - provides upgrade-related archetype components via IServiceUpgrade. This component is intended to be attached to a Building prefab (via the ComponentMenu attribute) and requires a CityServiceBuilding component.


Fields

  • public ResourceProductionInfo[] m_Resources
    Array of resource production entries that define which resources the building produces, the production rate and storage capacity for each resource. During Initialize, each entry is converted into a ResourceProductionData and written into the entity's DynamicBuffer using EconomyUtils.GetResource(...) to resolve the resource identifier.

Properties

  • (none)
    This component exposes no public properties. Configuration is done via the public field(s) and methods.

Constructors

  • public ResourceProducer()
    Default parameterless constructor (implicit). Typical instances are created by Unity when attaching the component to a prefab.

Methods

  • public override void GetPrefabComponents(HashSet<ComponentType> components)
    Adds the component types required on the prefab/entity for resource production. Specifically adds ComponentType.ReadWrite() so the entity will include a DynamicBuffer.

  • public override void GetArchetypeComponents(HashSet<ComponentType> components)
    Called when building the entity archetype for this prefab. If the prefab/gameObject does not contain a ServiceUpgrade component (checked via GetComponent() == null), this method adds the ECS components Game.Economy.Resources and Game.Buildings.ResourceProducer as ReadWrite component types to the archetype.

  • public void GetUpgradeComponents(HashSet<ComponentType> components)
    IServiceUpgrade implementation. Adds archetype component types that should be present for upgraded versions of the building: Game.Economy.Resources and Game.Buildings.ResourceProducer (both ReadWrite).

  • public override void Initialize(EntityManager entityManager, Entity entity)
    Populates the entity's DynamicBuffer from the m_Resources array. Implementation details:

  • If m_Resources is non-null, it gets the DynamicBuffer for the entity and resizes it to match m_Resources.Length.
  • For each ResourceProductionInfo in m_Resources it writes a ResourceProductionData constructed with:
    • EconomyUtils.GetResource(resourceInfo.m_Resource) — resolves the resource id,
    • resourceInfo.m_ProductionRate,
    • resourceInfo.m_StorageCapacity.

This method ensures the ECS entity has the initial resource production configuration required by the game simulation.

Usage Example

// Example: configure a ResourceProducer in code (typical usage is via prefab inspector)
[RequireComponent(typeof(CityServiceBuilding))]
public class MyCustomServiceBuilding : MonoBehaviour
{
    public ResourceProducer resourceProducer;

    void Reset()
    {
        // Configure two resources in the inspector or via script
        resourceProducer = gameObject.AddComponent<ResourceProducer>();
        resourceProducer.m_Resources = new[]
        {
            new ResourceProductionInfo { m_Resource = "Water", m_ProductionRate = 10f, m_StorageCapacity = 100f },
            new ResourceProductionInfo { m_Resource = "Electricity", m_ProductionRate = 5f, m_StorageCapacity = 50f }
        };
    }
}

// At entity creation the ResourceProducer.Initialize will be called by the prefab->entity conversion.
// It writes ResourceProductionData entries into the entity's DynamicBuffer<ResourceProductionData>.

Notes and tips: - The ComponentMenu attribute places this component under "Buildings/" in the Unity add-component menu. The component requires a CityServiceBuilding component on the same GameObject. - If you want upgrade behavior, add a ServiceUpgrade component to the prefab; GetArchetypeComponents will then skip adding the runtime Game.Economy.Resources / Game.Buildings.ResourceProducer components and GetUpgradeComponents will be used for upgrade archetypes.