Search Unity

Feedback Batched instantiation proposal

Discussion in 'Entity Component System' started by temps12, Apr 28, 2019.

  1. temps12

    temps12

    Joined:
    Nov 28, 2014
    Posts:
    41
    So I've been generating a world lately and instantiate a lot of prefabs. I've tried a few solutions to instantiate as fast as possible. The best solution I have found so far is creating a job that fills a NativeList with data about which prefab, position and rotation it should have. Then I batch instantiate and lastly I make a new job that set the position and rotation of the newly created entities.

    Both jobs are Bursted which is cool, but when setting the component data its not doing it linear. It’s doing it with ComponentDataFromEntity. So while it is still a fast look up there are a few cache misses every time I set a component.

    I have a proposal of a way of instantiating batches. The code could look something like this:

    Code (CSharp):
    1. // NativeArray<Translation> positions;
    2. // NativeArray<Rotation> rotations;
    3. // positions & rotations are filled from a job. The have the same lenght.
    4.  
    5. NativeArray<Entity> newEntities = new NativeArray<Entity>(positions.Length, Allocator.Temp);
    6.  
    7. EntityManager.BeginBatch();
    8. EntityManager.SetBatchedComponentData(positions);
    9. EntityManager.SetBatchedComponentData(rotations);
    10. EntityManager.InstantiateBatch(prefab, newEntities);
    The idea is that positions and rotations will be copied into the chunk when instantiating instead of set after.

    There is also ExclusiveEntityTransaction which I will probably use in the end and while it is threaded the same problem still exists there from what I've seen. I think I've read somewhere that Unity will extend batching and maybe something somewhat like this is already on the way?

    Is there something that I've missed that is currently supported that I could do better maybe?