How to instantiate multiple entities from a prefab during Bake() in a Baker ?

Imagine you have a Grid baker that should populate various prefabs when adjusted, these all need to be entities. It needs to be reactive in scene view.

How would you go about doing this using the Baker class? there is no way to make use of
RenderMeshUtility.AddComponents and the other RenderMeshUtility methods are internal.

Would an ISystem using [WorldSystemFilter(WorldSystemFilterFlags.BakingSystem)] be the correct approach?

How do you get around the duplicate entity guids from instantiating the prefabs when baking?

Call GetEntity() on each unique tile prefab, and then store the returned entity references in your grid in a DynamicBuffer. Then at runtime instantiate those entities.

thanks for the reply, I’m trying to do it in edit mode, because I want to see the results while editing.
I’ve changed my approach to use a BakingSystem but now the entities only render when the SubScene is closed.
When the SubScene is open, I can see that the entities and components are created, but they don’t render which is odd.

here is the general idea with the Grid authoring,

I use GridCell to designate which entities created by the baker need to be operated on further in the baking system (because I cannot access RenderMeshUtility.AddComponents in a Baker)

[TemporaryBakingType]
public struct GridCell : IComponentData
{
    public float3 Value;
    public UnityObjectRef<Mesh> mesh;
    public UnityObjectRef<Material> material;
}

public class GridAuthoringBaker : Baker<GridAuthoring>
{
    public override void Bake(GridAuthoring authoring)
    {
        var aabb = authoring.aabb;

        var entity = GetEntity(authoring, TransformUsageFlags.WorldSpace);
        // was trying to pass this in.. but cannot instantiate during Baking due to duplicate GUID issue
        //var cellPrefab = GetEntity(authoring.prefab, TransformUsageFlags.Renderable);
      
        var grid = new Grid { aabb = aabb, dimension = (int3)(aabb.Max - aabb.Min) };
        AddComponent(entity, grid );

        // create baking entities... ????
        for (int i = 0; i < grid.Length; i++)
        {
            var e = CreateAdditionalEntity(TransformUsageFlags.WorldSpace, false, $"{authoring.name}_{i.ToString()}");
          
            var pos = Util.ConvertIndexToWorldPosition(i, grid.dimension, grid.aabb);
          
            Debug.DrawRay(pos,Vector3.up,Color.cyan,1f);
          
            AddComponent(e, new GridCell { Value = pos, mesh = authoring.mesh, material = authoring.material});
        }
      
    }
}

And here is the baking system:

[WorldSystemFilter(WorldSystemFilterFlags.BakingSystem)]
public partial struct GridBakeSystem : ISystem
{
    private EntityQuery _gridCellQuery;

    public void OnCreate(ref SystemState state)
    {
        _gridCellQuery = new EntityQueryBuilder(Allocator.Temp)
            .WithAll<GridCell>()
            .WithOptions(EntityQueryOptions.IncludePrefab | EntityQueryOptions.IncludeDisabledEntities)
            .Build(ref state);
    }

    [BurstCompile]
    public void OnUpdate(ref SystemState state)
    {
        var cells = _gridCellQuery.ToComponentDataArray<GridCell>(Allocator.TempJob);
        var entities = _gridCellQuery.ToEntityArray(Allocator.TempJob);
      
        for (var i = 0; i < cells.Length; i++)
        {
            var entity = entities[i];
            //var entity = state.EntityManager.CreateEntity();
            var cell = cells[i];
          
            var desc = new RenderMeshDescription(shadowCastingMode: ShadowCastingMode.Off, receiveShadows: false);
            var renderMeshArray = new RenderMeshArray(new [] { cell.material.Value }, new [] { cell.mesh.Value });
          
            RenderMeshUtility.AddComponents(
                entity,
                state.EntityManager,
                desc,
                renderMeshArray,
                MaterialMeshInfo.FromRenderMeshArrayIndices(0, 0));
      
            state.EntityManager.AddComponentData(entity, new LocalToWorld{Value = float4x4.TRS(cell.Value,quaternion.identity,new float3(1f,1f,1f) * Util.CELL_RADIUS)});
            state.EntityManager.AddComponentData(entity, new CellInfluence{delta = 1f,value = 0f});
            state.EntityManager.AddBuffer<CellNeighbors>(entity);
            state.EntityManager.AddBuffer<PropagationValue>(entity);
        }
      
        entities.Dispose(state.Dependency);
        cells.Dispose(state.Dependency);
    }
}

I think really what you want is a system that does the procedural spawning in the Editor World. You can make a runtime system run in the Editor World via WorldSystemFilter attribute.