docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Introducing dynamic buffer components

    A dynamic buffer component is a component that acts as a resizable array of unmanaged structs. You can use it to store array data for an entity, such as waypoint positions for the entity to navigate between.

    Alongside the data, each buffer stores a Length, a Capacity, and an internal pointer:

    • The Length is the number of elements in the buffer. It starts at 0 and increments when you append a value to the buffer.
    • The Capacity is the amount of storage in the buffer. It starts out matching the internal buffer capacity. Setting Capacity resizes the buffer.
    • The pointer indicates where the dynamic buffer data is. Initially it is null to signify that the data is in the chunk with the entity, and if Unity moves the data outside the chunk, the pointer is set to point to the new array. For more information on how Unity stores dynamic buffer components, see Capacity.

    Capacity

    The initial capacity of a dynamic buffer is defined by the type that the buffer stores. By default, the capacity defaults to the number of elements that fit within 128 bytes. For more information, see DefaultBufferCapacityNumerator. You can specify a custom capacity using the InternalBufferCapacity attribute. For information on how to create a dynamic buffer component type, see Create a dynamic buffer component type.

    Initially, Unity stores the dynamic buffer data directly in the chunk of the entity that the component belongs to. If the length of a dynamic buffer is ever greater than the capacity, Unity copies the dynamic buffer data to an array outside of the chunk. If the length of the dynamic buffer later shrinks to less than the capacity, Unity still stores the data outside of the chunk; if Unity moves dynamic buffer data outside of a chunk, it never moves the data back into the chunk.

    The original internal buffer capacity is part of the chunk and Unity only deallocates it when Unity deallocates the chunk itself. This means if the dynamic buffer length exceeds the internal capacity and Unity copies the data outside of the chunk, there is wasted space within the chunk. It's best practice to use the data in the chunk when possible. To do this, make sure most of your entities don't exceed the buffer capacities, but also don't set the capacity too high if the entities don't use it. If the size of a dynamic buffer changes too much, it's best practice to store its data outside of the chunk. To do this, set the InternalBufferCapacity to 0.

    There are other options available to store array data:

    • Blob assets: Stores tightly-packed read-only structured data, including arrays, and multiple entities can share a single blob asset. Because they're read-only, you can access them from multiple threads simultaneously.
    • Managed components: Stores arrays of native or managed objects. However, accessing managed component data is more restrictive and less performant than dynamic buffer component data. Also you need to manually clone and dispose of the array data.
    • Shared components: Similar to managed components, they store arrays of native or managed objects and your entities can store indices into these larger arrays. They have the same restrictions and performance considerations as managed components.

    Structural changes

    Structural changes might destroy or move the array referenced by a dynamic buffer which means that any handle to a dynamic buffer becomes invalid after a structural change. You must reacquire dynamic buffers after any structural changes. For example:

    public void DynamicBufferExample(Entity e)
    {
        // Acquires a dynamic buffer of type MyElement.
        DynamicBuffer<MyElement> myBuff = EntityManager.GetBuffer<MyElement>(e);
    
        // This structural change invalidates the previously acquired DynamicBuffer.
        EntityManager.CreateEntity();
    
        // A safety check will throw an exception on any read or write actions on the buffer.
        var x = myBuff[0];
    
        // Reacquires the dynamic buffer after the above structural changes.
        myBuff = EntityManager.GetBuffer<MyElement>(e);
        var y = myBuff[0];
    }
    

    Comparison to native containers in components

    Dynamic buffers are often preferable to using nativecontainers on components because they are free of the job scheduling restrictions on the latter, and because they can optionally be stored inline inside a chunk, which helps reduce memory bandwidth usage.

    In general, when there is more than one entity that needs a collection on it, it probably merits a dynamic buffer. If there's only one, it might work well as a singleton with a native container on it.

    Additional resources

    • Create a dynamic buffer component

    Did you find this page useful? Please give it a rating:

    Thanks for rating this page!

    Report a problem on this page

    What kind of problem would you like to report?

    • This page needs code samples
    • Code samples do not work
    • Information is missing
    • Information is incorrect
    • Information is unclear or confusing
    • There is a spelling/grammar error on this page
    • Something else

    Thanks for letting us know! This page has been marked for review based on your feedback.

    If you have time, you can provide more information to help us fix the problem faster.

    Provide more information

    You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:

    You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:

    You've told us there is information missing from this page. Please tell us more about what's missing:

    You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:

    You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:

    You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:

    You've told us this page has a problem. Please tell us more about what's wrong:

    Thank you for helping to make the Unity documentation better!

    Your feedback has been submitted as a ticket for our documentation team to review.

    We are not able to reply to every ticket submitted.

    In This Article
    • Capacity
    • Structural changes
    • Comparison to native containers in components
    • Additional resources
    Back to top
    Copyright © 2025 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)