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. Dismiss Notice

Getting blobArray lengths before processing in EntitiesForEach()

Discussion in 'Entity Component System' started by RoughSpaghetti3211, Dec 24, 2020.

  1. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,695
    Im running into a constant wall that I hope someone can help me with. For my latest example, Im building a mesh, one approach would be to pass each triangle as an entity which works great, all the
    EntityQuery allows me to retrieve the number of vtx and tri before I pass them to a native array for building. The problem is I end with a lot of entities.

    So I group my triangles on several entities eg one patch entity has say 100- 150 triangles. The problem with this is passing data around become really hard because I don't know how to get the number of triangles on each entity before my EntitiesForeach. The triangles data is stored as blobs on each entity.

    What am I missing here, why does this approach feel like such an uphill battle. Am I missing an easy way to get the len of my blob arrays on each entity?
     
  2. Lieene-Guo

    Lieene-Guo

    Joined:
    Aug 20, 2013
    Posts:
    547
    This happens all the time, with any dynamic size container set. You don't get the exact size before iterate over all the containers. allocate a native list with an upper limit capacity. And use native list parallax writer to fill the list in job.
    and after that build your mesh with the data in the list.
     
  3. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,984
    I would first try a single-threaded job that iterates through the blobs and sums the number of triangles, then resizes a NativeList.