Skip to content

Game.Prefabs.MeshIndex

Assembly: Assembly-CSharp.dll
Namespace: Game.Prefabs

Type: struct

Base: Unity.Entities.IBufferElementData

Summary:
MeshIndex is a small IBufferElementData struct used to store a single mesh index (vertex/index reference) inside an ECS DynamicBuffer. It is marked with [InternalBufferCapacity(0)] so buffer storage is purely dynamic. The type includes helper Unpack methods to convert raw index data (byte arrays) into MeshIndex entries, handling both 16-bit and 32-bit index formats and optionally applying a vertex offset when converting.


Fields

  • public int m_Index
    Stores the integer index value. Represents the resolved index (optionally already adjusted by a vertexOffset passed to Unpack). This is the only payload carried by the buffer element.

Properties

  • None

Constructors

  • public MeshIndex(int index)
    Constructs a MeshIndex with the given index value and stores it in m_Index.

Methods

  • public static void Unpack(NativeArray<byte> src, DynamicBuffer<MeshIndex> dst, int count, IndexFormat format)
    Resizes the provided DynamicBuffer to count elements (using ResizeUninitialized) and delegates to the other Unpack overload with a vertexOffset of 0. Use this to convert raw index bytes into a DynamicBuffer.

  • public static void Unpack(NativeArray<byte> src, NativeArray<MeshIndex> dst, int count, IndexFormat format, int vertexOffset)
    Interprets the src byte array as either 32-bit (IndexFormat.UInt32) or 16-bit indices and fills dst (a NativeArray) with MeshIndex instances. For 32-bit indices the bytes are reinterpreted as ints; for 16-bit indices they are reinterpreted as ushorts. Each read index is incremented by vertexOffset before being stored. Note: the implementation reinterprets the entire src array and iterates based on the resulting native array length, so count and the size of src should match (i.e., src should contain count indices in the specified format).

Behavior / notes: - Uses NativeArray.Reinterpret to avoid allocations and to convert the raw bytes into the correct integer type. - The outer Unpack resizes the DynamicBuffer with ResizeUninitialized — any existing buffer contents will be discarded. - Intended for converting mesh index buffers (as bytes) into ECS-friendly MeshIndex buffers for later processing in jobs/systems.

Usage Example

// Example: unpack a byte array holding index data into an entity's DynamicBuffer<MeshIndex>
NativeArray<byte> rawIndexBytes = /* obtained from mesh or asset */ ;
Entity entity = /* entity that has a DynamicBuffer<MeshIndex> */ ;
var buffer = EntityManager.GetBuffer<MeshIndex>(entity);

// If indices are 16-bit:
MeshIndex.Unpack(rawIndexBytes, buffer, count: indexCount, format: IndexFormat.UInt16);

// If indices are 32-bit:
MeshIndex.Unpack(rawIndexBytes, buffer, count: indexCount, format: IndexFormat.UInt32);

// Low-level example using NativeArray<MeshIndex> and a vertex offset:
NativeArray<MeshIndex> dst = new NativeArray<MeshIndex>(indexCount, Allocator.Temp);
MeshIndex.Unpack(rawIndexBytes, dst, indexCount, IndexFormat.UInt16, vertexOffset: 100);
// use dst...
dst.Dispose();

Additional tips: - Ensure that the provided count matches the number of indices encoded in src for the chosen IndexFormat, otherwise you may get mismatched lengths or truncated data. - This helper is useful when importing mesh index buffers into ECS for rendering, collision, or other mesh-processing systems.