docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    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);
    } 
    

    Additional resources

    • Access dynamic buffers from jobs
    • Reinterpret a dynamic buffer
    In This Article
    Back to top
    Copyright © 2024 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)