Skip to content

Game.SubArea

Assembly:
Assembly-CSharp (typical for Unity projects / game code; may vary depending on build)

Namespace: Game.Prefabs

Type:
struct (IBufferElementData)

Base:
Unity.Entities.IBufferElementData

Summary:
Represents a buffer element used to associate a prefab Entity with a numeric node range. Marked with InternalBufferCapacity(1) so each entity's dynamic buffer will reserve space for one element in the chunk by default. Typical use is to store per-entity sub-area information (a prefab reference plus an int2 describing a range or bounds in node/index space). The struct is blittable and intended for use with Unity's ECS dynamic buffers.


Fields

  • public Entity m_Prefab
    Holds an Entity reference to the prefab associated with this sub-area. This is typically a prefab Entity created/managed elsewhere and used for instantiation or lookup.

  • public int2 m_NodeRange
    A 2-component integer vector (Unity.Mathematics.int2) representing a node range. Commonly used as [startIndex, endIndex] or [min, max] for indexing or bounding nodes within the sub-area. Interpretation depends on the system that consumes this buffer element.

Properties

  • This type defines no properties. It is a plain data container (IBufferElementData) with public fields.

Constructors

  • public SubArea()
    Default value-type constructor generated by the runtime. When added to a buffer without explicit initialization, fields will have default values (m_Prefab = Entity.Null, m_NodeRange = int2(0,0)). Initialize fields explicitly when adding instances.

Methods

  • This struct declares no methods. Behavior is provided by consumers/systems and by Unity.Entities for buffer management.

Usage Example

// Example: add a SubArea element to an entity's dynamic buffer in a SystemBase or ISystem context
using Unity.Entities;
using Unity.Mathematics;

public partial class SubAreaExampleSystem : SystemBase
{
    protected override void OnCreate()
    {
        base.OnCreate();
    }

    protected override void OnUpdate()
    {
        var em = EntityManager;
        Entity prefabEntity = /* obtain or create prefab entity */ Entity.Null;

        Entities
            .WithNone<SomeMarkerComponent>() // example filter
            .ForEach((Entity e, int entityInQueryIndex) =>
            {
                // Ensure the entity has a buffer
                if (!em.HasComponent<DynamicBuffer<SubArea>>(e))
                {
                    em.AddBuffer<SubArea>(e);
                }

                var buffer = em.GetBuffer<SubArea>(e);
                var element = new SubArea
                {
                    m_Prefab = prefabEntity,
                    m_NodeRange = new int2(10, 20) // example start/end
                };
                buffer.Add(element);
            }).WithoutBurst().Run();
    }
}

Notes: - InternalBufferCapacity(1) reserves space for 1 element in-chunk; adding more elements will allocate overflow memory. Choose the attribute value based on expected average element count to reduce allocations. - Interpret m_NodeRange consistently across systems (e.g., inclusive/exclusive end).