Hi, I have the following loop which resides in OnUpdate in a System.
It makes an EntityCommandBuffer, loops over a certain set of entities, uses the buffer to remove two components and add one, does other stuff, and then plays back the ecb after the loop.
The strange behaviour im encountering, is the ECB actions are only applying for the very first iteration of the loop, on the very first frame. The entities still get looped over, and everything else in the loop (all the mesh stuff) still happens and works like normal, but only the very first entity on the very first frame gets it's ChunkUnrenderedTag and ChunkQueuedForRenderTag removed and ChunkRenderedTag added. Am I using the entitycommandbuffer wrong here?
Code:
//register meshes
EntityCommandBuffer ecb = new EntityCommandBuffer(Allocator.Temp);
foreach ((RefRO<Chunk> chk, RefRO<ChunkUnrenderedTag> cut, RefRO<ChunkQueuedForRenderTag> cqfrt, RefRO<ChunkMeshData> CMD, RefRW<MaterialMeshInfo> mmi) in SystemAPI.Query<RefRO<Chunk>, RefRO<ChunkUnrenderedTag>, RefRO<ChunkQueuedForRenderTag>, RefRO<ChunkMeshData>, RefRW<MaterialMeshInfo>>())
{
ecb.RemoveComponent<ChunkUnrenderedTag>(chk.ValueRO.selfEntity);
ecb.RemoveComponent<ChunkQueuedForRenderTag>(chk.ValueRO.selfEntity);
Mesh mesh = new Mesh();
List<Vector3> vertices = new List<Vector3>();
List<int> triangles = new List<int>();
for (int i = 0; i < CMD.ValueRO.vertices.Length; i++)
{
vertices.Add(new Vector3(CMD.ValueRO.vertices[i].x, CMD.ValueRO.vertices[i].y, CMD.ValueRO.vertices[i].z));
}
for (int i = 0; i < CMD.ValueRO.triangles.Length; i++)
{
triangles.Add(CMD.ValueRO.triangles[i]);
}
mesh.vertices = vertices.ToArray();
mesh.triangles = triangles.ToArray();
Material material = new Material(Shader.Find("Universal Render Pipeline/Lit"));
EntitiesGraphicsSystem egs = state.World.GetExistingSystemManaged<EntitiesGraphicsSystem>();
BatchMeshID meshIndex = egs.RegisterMesh(mesh);
BatchMaterialID materialIndex = egs.RegisterMaterial(material);
Debug.Log($"bmid {meshIndex}");
mmi.ValueRW.Mesh = (int)meshIndex.value;
mmi.ValueRW.Material = (int)materialIndex.value;
ecb.AddComponent<ChunkRenderedTag>(chk.ValueRO.selfEntity);
}
ecb.Playback(state.EntityManager);