Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Can't See Spawned Entities

Discussion in 'Entity Component System' started by spectre1989, Oct 3, 2019.

  1. spectre1989

    spectre1989

    Joined:
    Oct 6, 2009
    Posts:
    125
    Hey all,

    Sorry to make a "help me fix my code" post, I'm sure I'm just missing something silly.

    I've been trying to do a more up-to-date version of the ECS tutorial where you spawn a load of ships which fly from one end of the screen to the other and loop around. So far I'm just doing the spawning bit (I'm basing it on the Spawner_FromEntity sample from github here).

    My component:
    Code (CSharp):
    1. // Spawner.cs
    2.  
    3. using Unity.Entities;
    4.  
    5. public struct Spawner : IComponentData
    6. {
    7.     public int initial_ship_count;
    8.     public int increment_ship_count;
    9.     public float ship_movement_speed;
    10.     public float top_bound;
    11.     public float bottom_bound;
    12.     public float left_bound;
    13.     public float right_bound;
    14.     public Entity ship_prefab;
    15. }
    The authoring monobehaviour:
    Code (CSharp):
    1. using Unity.Entities;
    2. using UnityEngine;
    3. using System.Collections.Generic;
    4.  
    5. [RequiresEntityConversion]
    6. public class SpawnerAuthoring : MonoBehaviour, IDeclareReferencedPrefabs, IConvertGameObjectToEntity
    7. {
    8.     public int initial_ship_count;
    9.     public int increment_ship_count;
    10.     public float ship_movement_speed;
    11.     public float top_bound;
    12.     public float bottom_bound;
    13.     public float left_bound;
    14.     public float right_bound;
    15.     public GameObject ship_prefab;
    16.  
    17.     public void DeclareReferencedPrefabs(List<GameObject> referenced_prefabs)
    18.     {
    19.         referenced_prefabs.Add(this.ship_prefab);
    20.     }
    21.  
    22.     public void Convert(Entity entity, EntityManager manager, GameObjectConversionSystem conversion_system)
    23.     {
    24.         Spawner spawner = new Spawner
    25.         {
    26.             initial_ship_count = initial_ship_count,
    27.             increment_ship_count = increment_ship_count,
    28.             ship_movement_speed = ship_movement_speed,
    29.             top_bound = top_bound,
    30.             bottom_bound = bottom_bound,
    31.             left_bound = left_bound,
    32.             right_bound = right_bound,
    33.             ship_prefab = conversion_system.GetPrimaryEntity(ship_prefab)
    34.         };
    35.         manager.AddComponentData(entity, spawner);
    36.     }
    37. }
    The system:
    Code (CSharp):
    1. using Unity.Entities;
    2. using Unity.Jobs;
    3. using Unity.Transforms;
    4. using Unity.Mathematics;
    5. using Random = Unity.Mathematics.Random;
    6. using URandom = UnityEngine.Random;
    7. using UnityEngine;
    8. using Unity.Collections;
    9.  
    10. [UpdateInGroup(typeof(SimulationSystemGroup))]
    11. public class SpawnerSystem : JobComponentSystem
    12. {
    13.     private bool initial_spawn_done;
    14.     private BeginInitializationEntityCommandBufferSystem entity_command_buffer_system;
    15.  
    16.     protected override void OnCreate()
    17.     {
    18.         this.initial_spawn_done = false;
    19.         this.entity_command_buffer_system = this.World.GetOrCreateSystem<BeginInitializationEntityCommandBufferSystem>();
    20.     }
    21.  
    22.     struct SpawnJob : IJobForEachWithEntity<Spawner>
    23.     {
    24.         public EntityCommandBuffer.Concurrent command_buffer;
    25.         public bool is_initial;
    26.         public Random random;
    27.  
    28.         public void Execute(Entity entity, int index, [ReadOnly] ref Spawner spawner)
    29.         {
    30.             int count = this.is_initial ? spawner.initial_ship_count : spawner.increment_ship_count;
    31.             for (int i = 0; i < count; ++i)
    32.             {
    33.                 Entity instance = command_buffer.Instantiate(index, spawner.ship_prefab);
    34.  
    35.                 command_buffer.SetComponent(index, instance,
    36.                     new Translation{ Value = new float3(
    37.                         random.NextFloat(spawner.left_bound, spawner.right_bound),              // x
    38.                         0.0f,                                                                   // y
    39.                         random.NextFloat(spawner.bottom_bound - 5.0f, spawner.bottom_bound)) });// z
    40.             }
    41.         }
    42.     }
    43.  
    44.     protected override JobHandle OnUpdate(JobHandle inputDeps)
    45.     {
    46.         if (!this.initial_spawn_done)
    47.         {
    48.             this.initial_spawn_done = true;
    49.  
    50.             JobHandle job = new SpawnJob
    51.             {
    52.                 command_buffer = this.entity_command_buffer_system.CreateCommandBuffer().ToConcurrent(),
    53.                 is_initial = true,
    54.                 random = new Random((uint)URandom.Range(int.MinValue, int.MaxValue))
    55.             }.Schedule(this, inputDeps);
    56.  
    57.             this.entity_command_buffer_system.AddJobHandleForProducer(job);
    58.  
    59.             return job;
    60.         }
    61.         else if (Input.GetKeyDown(KeyCode.Space))
    62.         {
    63.             JobHandle job = new SpawnJob
    64.             {
    65.                 command_buffer = this.entity_command_buffer_system.CreateCommandBuffer().ToConcurrent(),
    66.                 is_initial = false,
    67.                 random = new Random((uint)URandom.Range(int.MinValue, int.MaxValue))
    68.             }.Schedule(this, inputDeps);
    69.  
    70.             this.entity_command_buffer_system.AddJobHandleForProducer(job);
    71.  
    72.             return job;
    73.         }
    74.  
    75.         return new JobHandle();
    76.     }
    77. }
    Here's my scene hierarchy:


    The Spawner GameObject:

    And the prefab I'm using:


    Any help much appreciated
    Cheers
     
  2. ndesy

    ndesy

    Joined:
    Jul 22, 2019
    Posts:
    20
    What's the problem?
     
  3. spectre1989

    spectre1989

    Joined:
    Oct 6, 2009
    Posts:
    125
    I can't see the spawned entities. I can step through the spawning jobs in VS so I'm pretty sure they're being spawned, just I can't see them... In the editor or the game viewport.
     
    bugfinders likes this.
  4. ndesy

    ndesy

    Joined:
    Jul 22, 2019
    Posts:
    20
    That should work. Did you install the Hybrid Renderer package?
     
    spectre1989 likes this.
  5. spectre1989

    spectre1989

    Joined:
    Oct 6, 2009
    Posts:
    125
    Woo! That's fixed it, thanks mate :)
     
    ndesy likes this.
  6. spectre1989

    spectre1989

    Joined:
    Oct 6, 2009
    Posts:
    125
    Strangely I'm quite a way off the entity count the guy manages in the tutorial. He gets to > 100k entities at 30fps, I'm more like 30k entities. I wonder why that is..