Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Actual PROPER way to instantiate many entities at once?

Discussion in 'Entity Component System' started by flatterino, Mar 25, 2020.

  1. flatterino

    flatterino

    Joined:
    Jan 22, 2018
    Posts:
    17
    Hello,

    The documentation, tutorials, and my searches all give me contradictory information regarding the spawning of many entities while taking advantage of jobs and threads. I'm confused.

    For simplicity, let's say that, for whatever reason, I want to instantiate 50000 entities at once. I want to do this in pure ECS: not a single MonoBehavior involved or weird hybrid authoring at all. What's the most performant/correct way to do this as of today?

    Things I've heard:
    • You use World.Active.EntityManager.CreateEntity(). But this can be only called from the main thread anyway.
    • You use EntityCommandBuffer to queue the creation of the entities. It will run at the end of the frame. But then again it feels pointless, because the actual creation of the entities will simply be queued to run "one-by-one" in the main thread.
    • You use EntityCommandBuffer with EntityQuery (seen here). I don't understand how this would improve things since we still depend on queuing the instantiating of the entities and running them one-by-one in the main thread.
    I've seen some other approaches but frankly, none of them make total sense to me. I can get them all working, but something feels wrong.

    What am I missing? Is entity composition and instantiation simply supposed to be done in the main thread, and I'm just overthinking this?

    I appreciate your help.
     
  2. wg-siggig

    wg-siggig

    Joined:
    Mar 17, 2020
    Posts:
    36
    From what I've heard using CreateEntity() with the NativeArray overload is still the fastest way.
     
    flatterino likes this.
  3. Curlyone

    Curlyone

    Joined:
    Mar 15, 2018
    Posts:
    41
    I am not a pro, but creating entities(structral change) causes chunks to be re calculated, thats why it is being done in main thread.
     
    flatterino likes this.
  4. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,647
    Batch operations on EntityManager by far the fastest way to create lots of entities or set lots of components - order of magnitude faster than ECB
     
    flatterino likes this.
  5. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    the batch api's being referred to are the following:

    public void EntityManager.Instantiate(Entity srcEntity, NativeArray<Entity> outputEntities);
    public NativeArray<Entity> EntityManager.Instantiate(Entity srcEntity, int instanceCount, Allocator allocator);

    As has been said, these are significantly faster than the non-batched versions.
     
    flatterino likes this.
  6. flatterino

    flatterino

    Joined:
    Jan 22, 2018
    Posts:
    17
    Thank you. I really appreciate everybody's help.