Search Unity

At the ECSSample\HelloCube\5. SpawnFromEntity

Discussion in 'Entity Component System' started by fanzhengyong2015, Sep 26, 2020.

  1. fanzhengyong2015

    fanzhengyong2015

    Joined:
    May 30, 2019
    Posts:
    26
    I don't understand why create Entity pre frame? There is a function OnUpdate() create entity where in the SpawnerSystem_FromEntity. This function was executed pre frame. Why not create one time. I know the code isn't error.It's just I don't know.
    Code (CSharp):
    1.     protected override void OnUpdate()
    2.     {
    3.         //Instead of performing structural changes directly, a Job can add a command to an EntityCommandBuffer to perform such changes on the main thread after the Job has finished.
    4.         //Command buffers allow you to perform any, potentially costly, calculations on a worker thread, while queuing up the actual insertions and deletions for later.
    5.         var commandBuffer = m_EntityCommandBufferSystem.CreateCommandBuffer().ToConcurrent();
    6.  
    7.         // Schedule the Entities.ForEach lambda job that will add Instantiate commands to the EntityCommandBuffer.
    8.         // Since this job only runs on the first frame, we want to ensure Burst compiles it before running to get the best performance (3rd parameter of WithBurst)
    9.         // The actual job will be cached once it is compiled (it will only get Burst compiled once).
    10.         Entities
    11.             .WithName("SpawnerSystem_FromEntity")
    12.             .WithBurst(FloatMode.Default, FloatPrecision.Standard, true)
    13.             .ForEach((Entity entity, int entityInQueryIndex, in Spawner_FromEntity spawnerFromEntity, in LocalToWorld location) =>
    14.         {
    15.             for (var x = 0; x < spawnerFromEntity.CountX; x++)
    16.             {
    17.                 for (var y = 0; y < spawnerFromEntity.CountY; y++)
    18.                 {
    19.                     var instance = commandBuffer.Instantiate(entityInQueryIndex, spawnerFromEntity.Prefab);
    20.  
    21.                     // Place the instantiated in a grid with some noise
    22.                     var position = math.transform(location.Value,
    23.                         new float3(x * 1.3F, noise.cnoise(new float2(x, y) * 0.21F) * 2, y * 1.3F));
    24.                     commandBuffer.SetComponent(entityInQueryIndex, instance, new Translation {Value = position});
    25.                 }
    26.             }
    27.  
    28.             commandBuffer.DestroyEntity(entityInQueryIndex, entity);
    29.         }).ScheduleParallel();
    30.  
    31.         // SpawnJob runs in parallel with no sync point until the barrier system executes.
    32.         // When the barrier system executes we want to complete the SpawnJob and then play back the commands (Creating the entities and placing them).
    33.         // We need to tell the barrier system which job it needs to complete before it can play back the commands.
    34.         m_EntityCommandBufferSystem.AddJobHandleForProducer(Dependency);
     
  2. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,705
    I can’t see the example properly but from memory there is a spawner entity, the lambda only runs when it find the spawner entity and the entity is destroy. So the code will only run once
     
  3. fanzhengyong2015

    fanzhengyong2015

    Joined:
    May 30, 2019
    Posts:
    26
    Thank you. I think I understand a bit. As your mean:
    In the line 28:
    commandBuffer.DestroyEntity(entityInQueryIndex, entity);

    It's remove entity(spawner entity), so it wouldn't execute lambda again when OnUpdate is executed at second time or later. As it's commond "....this job only runs on the first frame,..."