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

EntityManager.SetComponentData(NativeArray<Entity>,T[] componentData)

Discussion in 'Entity Component System' started by MNNoxMortem, May 8, 2020.

  1. MNNoxMortem

    MNNoxMortem

    Joined:
    Sep 11, 2016
    Posts:
    723
    What is the most efficient way to set the component data of all entities of a group/in the same chunk/created in the same NativeArray at once? Do i need to iterate over them? Should I use a job instead?

    I understand that Entities are just ids and do not necessarily lie together in memory but those inside of a chunk do as far as I understand it (isn't that the whole point?). What is the best way to update a whole chunk at once.

    Code (CSharp):
    1. EntityManager em = ...;
    2. int N = ...;
    3. for (var i = 0; i < N; i++)
    4. {
    5.   Entity e = x[i];
    6.   em.SetComponentData(e, new LocalToWorld());
    7.   em.SetComponentData<RenderMesh>(e, new RenderMesh());
    8. }
     
  2. Mockarutan

    Mockarutan

    Joined:
    May 22, 2011
    Posts:
    158
    With an EntityQuery if I'm not mistaken. Now, I did not realize this until now, but SetComponentData does not have an overloaded version that takes a EntityQuery, but maybe you can use AddComponentData instead?
     
  3. s_schoener

    s_schoener

    Unity Technologies

    Joined:
    Nov 4, 2019
    Posts:
    81
    In this case, you will want to use EntityManager.AddComponent<T>(NativeArray<Entity>) - this will internally figure out which of these entities are in the same chunk and add the components per chunk where possible.
     
    MNNoxMortem likes this.
  4. jonwah00

    jonwah00

    Joined:
    Jul 21, 2019
    Posts:
    36
    You could also create a single entity as a prefab and instantiate a native array based off that prefab, no?
     
  5. MNNoxMortem

    MNNoxMortem

    Joined:
    Sep 11, 2016
    Posts:
    723
    @s_schoener this does not really answer the question. I've asked how to set the values of the component data for an array. However, I do see how the initial code example might be misleading so consider this:
    Code (CSharp):
    1. EntityManager em = ...;
    2. int N = ...;
    3. LocalToWorld[] local2World =...;
    4. RenderMesh[] meshes = ...;
    5. for (var i = 0; i < N; i++)
    6. {
    7.   Entity e = x[i];
    8.   em.SetComponentData(e, local2World[i]);
    9.   em.SetComponentData<RenderMesh>(e, meshes[i]);
    10. }
    11.  
     
  6. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,655
    if your arrays elements aligned, and you have entity query for that type of entities, you can use
    CopyFromComponentDataArray(Async) and count of entities should be equal count of elements in array. You create NativeArray holders for your data and CopyFrom your arrays to NativeArrays and use that NativeArray for batch setup CD for all entities by CopyFromComponentDataArray. It's faster than set one by one because it just MemCpy of arrays to chunks directly at once.
     
    MNNoxMortem likes this.
  7. jonwah00

    jonwah00

    Joined:
    Jul 21, 2019
    Posts:
    36