Skip to content

Game.Prefabs.InitialResources

Assembly:
Assembly-CSharp (game assembly typically containing game code)

Namespace:
Game.Prefabs

Type:
class

Base:
ComponentBase, IServiceUpgrade

Summary:
InitialResources is a prefab component used by building prefabs (and building extensions) to declare a set of starting resources that should be added to an entity when the prefab is instantiated. It exposes an array of InitialResourceItem editable in the prefab, contributes appropriate ECS component types for prefab archetypes, and on Initialize it creates and populates a DynamicBuffer on the entity. The class is decorated with [ComponentMenu("Buildings/", ...)] so it appears under the Buildings menu in the editor.


Fields

  • public InitialResourceItem[] m_InitialResources
    Array of InitialResourceItem entries defined on the prefab. Each entry contains a ResourceStack (resource type + amount). During Initialize, each entry is converted into InitialResourceData and added to the entity's DynamicBuffer. The conversion resolves the resource reference via EconomyUtils.GetResource(...) so prefab references are mapped to the in-game Resource definitions.

Properties

  • None declared in this class.
    (The class exposes behavior via methods implementing the prefab/component pipeline rather than public properties.)

Constructors

  • public InitialResources()
    Implicit default constructor — no custom construction logic is defined in the source file. Instances are typically created/serialized by Unity when the prefab/component is edited or instantiated.

Methods

  • public override void GetPrefabComponents(HashSet<ComponentType> components)
    Adds ComponentType.ReadWrite() to the provided set. This tells the prefab processing that entities created from this prefab should have a DynamicBuffer component.

  • public override void GetArchetypeComponents(HashSet<ComponentType> components)
    If the prefab does not also have a ServiceUpgrade component (checked via GetComponent() == null), this method adds ComponentType.ReadWrite() to the archetype components. This means a standalone building prefab that defines initial resources will get a Resources component at archetype creation unless the prefab is part of a service upgrade where resource handling differs.

  • public void GetUpgradeComponents(HashSet<ComponentType> components)
    As part of the IServiceUpgrade implementation, this method adds ComponentType.ReadWrite() to the components for upgrade variants. This ensures upgraded versions that use this component also receive a Resources component.

  • public override void Initialize(EntityManager entityManager, Entity entity)
    Called when the prefab is initialized into an ECS entity. The method:

  • Calls base.Initialize(entityManager, entity)
  • Adds a DynamicBuffer to the entity via entityManager.AddBuffer(entity)
  • If m_InitialResources is non-null, iterates the array and for each entry:
    • Resolves the resource via EconomyUtils.GetResource(...) using the resource reference in the InitialResourceItem
    • Creates a ResourceStack with the resolved resource and the specified amount
    • Adds an InitialResourceData item containing that ResourceStack to the buffer

This method performs the mapping from editor-configured prefab data into runtime ECS buffer entries.

Usage Example

// Example: prefab-defined InitialResources will be processed at prefab initialization.
// The InitialResources component itself handles adding/pouplating InitialResourceData buffer.
// Below is an illustrative example of reading the added buffer from a system:

public class ApplyInitialResourcesSystem : SystemBase
{
    protected override void OnUpdate()
    {
        Entities
            .WithAll<InitialResourceData>() // or filter by some marker component
            .ForEach((Entity entity, DynamicBuffer<InitialResourceData> initResources) =>
            {
                // Apply initial resources to the entity's Resources component (if present)
                // Example pseudocode:
                // var resourcesBuffer = EntityManager.GetBuffer<Resources>(entity);
                // foreach (var r in initResources) { AddToResources(resourcesBuffer, r.m_Value); }
            }).WithoutBurst().Run();
    }
}

Notes and implementation tips: - The component relies on InitialResourceData, ResourceStack, Resources and EconomyUtils types from the game's economy/ECS code. Ensure those types are available when consuming this component in systems. - The GetArchetypeComponents check prevents adding a Resources component automatically when the prefab is part of a ServiceUpgrade; upgrades handle resource components via GetUpgradeComponents instead. - Because Initialize uses EntityManager.AddBuffer, consumers should expect a DynamicBuffer to exist on entities created from the prefab.