Create a dynamic buffer component
To create a dynamic buffer component, create a struct that inherits from IBufferElementData
. This struct defines the element of the dynamic buffer type and also represents the dynamic buffer Component itself.
To specify the initial capacity of the buffer, use the InternalBufferCapacity
attribute. For information on how Unity manages the capacity of the buffer, see Capacity.
The following code sample shows a buffer component:
[InternalBufferCapacity(16)]
public struct ExampleBufferComponent : IBufferElementData
{
public int Value;
}
Like other components, you can add a dynamic buffer component to an entity. However, you represent a dynamic buffer component with a DynamicBuffer
<ExampleBufferComponent>
and use dynamic buffer component-specific EntityManager
APIs, such as EntityManager.GetBuffer<T>
, to interact with them. For example:
public void GetDynamicBufferComponentExample(Entity e)
{
DynamicBuffer<ExampleBufferComponent> myDynamicBuffer = EntityManager.GetBuffer<ExampleBufferComponent>(e);
}