Game.Prefabs.InitialResourceData
Assembly:
Assembly-CSharp (game assembly — Unity/CS game code)
Namespace:
Game.Prefabs
Type:
struct
Base:
Unity.Entities.IBufferElementData
Summary:
InitialResourceData is a small ECS buffer element used on prefab entities to store one entry of starting/initial resources. Each element wraps a ResourceStack (likely a lightweight struct representing a resource type and an amount). Prefabs that need to provide initial inventories (for example, production buildings, warehouses, or transport vehicles) can carry a DynamicBuffer
Fields
public ResourceStack m_Value
Holds the resource entry for this buffer element. ResourceStack is expected to contain at least a resource identifier/type and an amount/quantity. When a prefab with an InitialResourceData buffer is instantiated, systems can iterate this buffer and apply the resource stacks to the created entity (or to linked storage entities).
Properties
- This struct defines no properties. It is a plain buffer element (field-backed) intended for use with DynamicBuffer
.
Constructors
public InitialResourceData()
(implicit)
As a value type (struct) there is an implicit parameterless constructor that default-initializes m_Value (ResourceStack will be default-initialized). No explicit constructors are defined in code.
Methods
- This struct defines no methods. It functions purely as a container (IBufferElementData) for use in ECS buffers.
Usage Example
// Example: Baker-based conversion (Entities authoring) to attach initial resources to a prefab entity.
using Unity.Entities;
using UnityEngine;
public class InitialResourceAuthoring : MonoBehaviour
{
public ResourceStack[] initialResources; // assign in inspector
class Baker : Baker<InitialResourceAuthoring>
{
public override void Bake(InitialResourceAuthoring authoring)
{
var e = GetEntity(TransformUsageFlags.None);
var buffer = AddBuffer<InitialResourceData>(e);
foreach (var rs in authoring.initialResources)
{
buffer.Add(new InitialResourceData { m_Value = rs });
}
}
}
}
// Example: Reading the buffer on spawn/initialization in a system
public partial class ApplyInitialResourcesSystem : SystemBase
{
protected override void OnUpdate()
{
Entities
.WithNone<ResourcesAppliedTag>() // example tag to run once
.ForEach((Entity entity, int entityInQueryIndex, DynamicBuffer<InitialResourceData> initBuf, ref StorageComponent storage) =>
{
foreach (var item in initBuf)
{
// Pseudocode: apply item.m_Value to storage
storage.AddResource(item.m_Value);
}
EntityManager.AddComponent<ResourcesAppliedTag>(entity);
}).WithoutBurst().Run();
}
}
Notes: - Add this buffer to prefab entities so conversion/initialization systems can populate runtime storage from the entries. - ResourceStack structure is game-specific; ensure its fields (resource id/type, amount) are filled correctly before adding to the buffer. - Use an appropriate one-time tag or conversion-time logic to avoid applying the initial resources multiple times.