0.51 Spawn system conversion to 1.0 and possible improvements.

I once made a system for spawning entities from a prefab for 0.51. How can it be converted to 1.0 now?

using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;

partial class SpawnerSystem : SystemBase
{
    private BeginSimulationEntityCommandBufferSystem beginSimulationEcbSystem;

    protected override void OnCreate()
    {
        beginSimulationEcbSystem = World.GetExistingSystem<BeginSimulationEntityCommandBufferSystem>();
    }
    protected override void OnUpdate()
    {
        EntityCommandBuffer ecb = beginSimulationEcbSystem.CreateCommandBuffer();

        EntityManager entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;

        Entities
            .ForEach((Entity e, in LevelSpawnerComponent levelSpawnerComponent) =>
            {
                Entity instance;
                for (int i = 0; i < levelSpawnerComponent.size.x; i++)
                {
                    instance = ecb.Instantiate(levelSpawnerComponent.prefab);
                    ecb.SetComponent(instance, new Translation { Value = new float3(i, 0, 0) });
                }

            }).WithoutBurst().Run();
        beginSimulationEcbSystem.AddJobHandleForProducer(Dependency);
    }
}

It is also interesting - how can this system be parallelized? ScheduleParallel() does not work with this spawner.

What do you mean converted to 1.0? SystemBase works fine in 1.0.
Do you mean convert to ISystem?

The biggest thing that would change is how you get the ecb:

var ecbSingleton = SystemAPI.GetSingleton<BeginSimulationEntityCommandBufferSystem.Singleton>();
var ecb = ecbSingleton.CreateCommandBuffer(state.WorldUnmanaged);

The job should work fine in parallel and with Burst. I can’t see LevelSpawnerComponent. It probably has a managed object that prevents Burst from compiling.
And to run in parallel you’d have to use CreateCommandBuffer().AsParallelWriter(). The parallel ecb requires an additional index. You can either use the entity.Index or entityInQueryIndex. More info here: Link

Also, because you have EntityManager entityManager. EntityManager is already part of SystemBase, you don’t have to get it. For ISystem the EM is located in SystemState state which is a parameter in every method you have to implement.

1 Like

Perhaps i should first deal with SystemBase and 0.51.
Here is my SpawnerAuthoring:

using Unity.Entities;
using UnityEngine;

[GenerateAuthoringComponent]
public struct SpawnerAuthoring : IComponentData
{
    public Vector3 size;
    public Entity prefab;
}
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;

partial class SpawnerSystem : SystemBase
{
    private BeginInitializationEntityCommandBufferSystem entityCommandBufferSystem;

    protected override void OnCreate()
    {
        entityCommandBufferSystem = World.GetOrCreateSystem<BeginInitializationEntityCommandBufferSystem>();
    }
    protected override void OnUpdate()
    {
        EntityCommandBuffer.ParallelWriter ecb = entityCommandBufferSystem.CreateCommandBuffer().AsParallelWriter();

        Entities
            .WithBurst()
            .ForEach((Entity entity, int entityInQueryIndex, in SpawnerAuthoring spawnerAuthoring) =>
            {
                for (int i = 0; i < spawnerAuthoring.size.x; i++)
                {
                    for (int j = 0; j < spawnerAuthoring.size.y; j++)
                    {
                        for (int k = 0; k < spawnerAuthoring.size.z; k++)
                        {
                            Entity instance = ecb.Instantiate(entityInQueryIndex, spawnerAuthoring.prefab);
                            ecb.SetComponent(entityInQueryIndex, instance, new Translation { Value = new float3(i, j, k) });
                        }
                    }
                }

                ecb.DestroyEntity(entityInQueryIndex, entity);
            }).ScheduleParallel();

        entityCommandBufferSystem.AddJobHandleForProducer(Dependency);
    }
}

I modified the system by including WithBurst and ScheduleParallel. But what exactly is the parallel here? If i run several of these spawners, will they work in parallel? Is it possible to make one spawner spawn prefabs in parallel? For example, if I want to spawn 100x100x100 then the spawn process itself takes quite a while. Is it possible to parallelize it?

1 Like