docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Entity command buffer overview

    An entity command buffer (ECB) stores a queue of thread-safe commands which you can add to and later play back. You can use an ECB to schedule structural changes from jobs and perform changes on the main thread after the jobs complete. You can also use ECBs on the main thread to delay changes, or play back a set of changes multiple times.

    The methods in EntityCommandBuffer record commands, which mirror methods available in EntityManager. For example:

    • CreateEntity(EntityArchetype): Registers a command that creates a new entity with the specified archetype.
    • DestroyEntity(Entity): Registers a command that destroys the entity.
    • SetComponent<T>(Entity, T): Registers a command that sets the value for a component of type T on the entity.
    • AddComponent<T>(Entity): Registers a command that adds a component of type T to the entity.
    • RemoveComponent<T>(EntityQuery): Registers a command that removes a component of type T from all entities that match the query.

    Temporary entities created by command buffers

    The entities returned from EntityCommandBuffer's CreateEntity() and Instantiate() methods are special; they don't fully exist until the command buffer is played back, but they can still be used in subsequent commands within the same command buffer. There are two valid uses for temporary entities:

    1. Commands can target temporary entities created by earlier commands in the same buffer. For example, it is valid to call EntityCommandBuffer.AddComponent<T>(e) on a temporary entity e created earlier in the same buffer.
    2. Unmanaged component values passed into commands can contain references to temporary entities. For example, EntityCommandBuffer.SetComponent(e2, new Parent{ Value = e}) is valid if e or e2 (or both) are temporary entities from the same command buffer. This includes IBufferElementData components (dynamic buffers) which contain entity fields.

    During command buffer playback, valid references to temporary entities created earlier in the buffer will be automatically replaced with a reference to the corresponding "real" entity. There is no way to determine which "real" entity corresponds to a given temporary entity after its command buffer has been played back.

    It is invalid to pass a temporary entity to an EntityManager method, or to reference a temporary entity from one command buffer in a different command buffer. Both cases will cause an exception to be thrown.

    Entity command buffer safety

    EntityCommandBuffer has a job safety handle, similar to a native container. This safety is only available in the Unity Editor, and not in player builds. The safety checks throw an exception if you try to do any of the following on an incomplete scheduled job that uses an ECB:

    • Access the EntityCommandBuffer through its AddComponent, Playback, Dispose, or other methods.
    • Schedule another job that accesses the same EntityCommandBuffer, unless the new job depends on the already scheduled job.
    Note

    It’s best practice to use a separate ECB for each distinct job. This is because if you reuse an ECB in consecutive jobs, the jobs might use an overlapping set of sort keys (such as if both use ChunkIndexInQuery), and the commands that the jobs record might be interleaved.

    Additional resources

    • Use an entity command buffer
    • Entity command buffer playback

    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
    • Temporary entities created by command buffers
    • Entity command buffer safety
    • Additional resources
    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)