Return indexed data IJobForEach

I have a system where the user can carve into a 2D mesh. Thing is, I don’t use RenderMesh because of unrelated reasons.

Since I can’t access UnityEngine mesh data outside of the main thread, I’d at least like to build the lists of vertices and triangles for all the chunks in a job, and then rebuild the meshes from that data on the main thread.

How do I return that data and bind it to its respective chunk? Nested native containers aren’t currently supported I’ve read.

I’d like to do something like that :

protected override JobHandle OnUpdate(JobHandle inputDependencies)
{

    NativeHashMap<int, NativeList<float3>> vertices = new NativeHashMap<int, NativeList<float3>>(480, Allocator.Temp);
    NativeHashMap<int, NativeList<int>> triangles = new NativeHashMap<int, NativeList<int>>(480, Allocator.Temp);

    MapMeshDataJob mapMeshDataJob = new MapMeshDataJob
    {
        MapBuffer = GetBufferFromEntity<IntBufferElement>(true),
        Vertices = vertices
        Triangles = triangles
    };

    JobHandle jobHandle = mapMeshDataJob.Schedule(inputDependencies);
    jobHandle.Complete();

    RebuildMeshes(vertices, triangles);

    vertices.Dispose();
    triangles.Dispose();

    return inputDependencies;
}

I’m trying to make sense of the DOTS and feeling pretty dense right now. If there’s anything wrong with the implementation as well, please let me know.

I recommend you to read this post. It handles building a mesh like your describe but UI/Text usage and the source code is available.

1 Like

Thank you for sharing. I found what I needed in his repo.

If anybody else stumbles upon this, wondering the same thing, I’m going with the following solution :

Instead of returning the data from the job, I’m just going to store the data in additional components and just read from them, post-job.