Search Unity

Populate scene with entities

Discussion in 'Entity Component System' started by Radu392, Oct 2, 2019.

  1. Radu392

    Radu392

    Joined:
    Jan 6, 2016
    Posts:
    210
    Is there a way to design levels using entities in the scene view for a 2D top down game? I already tried using gameobjects and converting them into entities at runtime, but having thousands of gameobjects in the scene slows down workflow too much.

    Specifically, I'm looking for a way to populate my 2D grid with tree and other resource entities in a visual way while completely avoiding gameobjects at design time (no procedural stuff). It doesn't have to be anything fancy, I could just settle with coloring the cells a certain color with each color representing a specific archetype of resource since a cell cannot contain more than one obstacle-type entity at once, then at runtime spawn the entities at those positions and set the relevant data. Note that my 2D grid is not using gameobjects or entities, but simply a 1D array of struct nodes.
     
  2. desertGhost_

    desertGhost_

    Joined:
    Apr 12, 2018
    Posts:
    260
  3. Radu392

    Radu392

    Joined:
    Jan 6, 2016
    Posts:
    210
    I read that already, sadly the link of the subscene example now throws a 404. But from what I understand, subscenes would still use game objects. They would just be placed in a different scene then loaded when needed right?
     
  4. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,264
    Subscenes use GameObjects for authoring when they are "open for authoring". The rest of the time they use Entities. The idea is you split up your scene into multiple subscenes and only have one subscene "open for authoring" at a time, effectively limiting the number of GameObjects to something your computer can handle.
     
    florianhanke likes this.
  5. Radu392

    Radu392

    Joined:
    Jan 6, 2016
    Posts:
    210
    That sounds cumbersome for a decently sized map (250k tiles), especially early in development when many variables can change, including said map size. I tried something else last night which I think works better for my case.

    For anyone interested, you can use a tilemap.

    Code (CSharp):
    1.      
    2. void SyncECSWithTilemap() {
    3.             Tilemap tilemap = GetComponentInChildren<Tilemap>();
    4.             BoundsInt bounds = tilemap.cellBounds;
    5.             TileBase[] tileArray = tilemap.GetTilesBlock(bounds);
    6.             for (int x = 0; x < bounds.size.x; x++) {
    7.                 for (int y = 0; y < bounds.size.y; y++) {
    8.                     TileBase tile = tileArray[x + y * bounds.size.x];
    9.                     if (tile != null && tile.name.Contains("Obstacle")) {
    10.                         // create the needed entities here
    11.                         tilemap.SetTile(new Vector3Int(x, y,0),null);
    12.                         setNodeToObstacle(x - size.x/2, y - size.y/2, true);
    13.                     }
    14.                 }
    15.             }
    16.         }
    17.  
    Tilemaps are also much more efficient at rendering stuff. I tried using the RenderMesh component to draw 1 million entities across 1 million tiles and that takes 70ms to do. The profiler spends most of its time on a step called 'Add batch'. If you use a tile map though, it takes exactly 0 extra ms to draw all those tiles. In both examples, only about 1-2k entities/tiles could be seen on screen. The tilemap also only adds 2 seconds of waiting to switch between scenes if it's 500 by 500 in size, which is very acceptable.

    So maybe using entities for static objects such as trees isn't the best idea if you've got tons of them. The best combination would probably be to use entities without the RenderMesh, but with other relevant data such as amount of wood etc and then use the tilemap for rendering..I'd still have to test how much it costs to remove/change a tile sprite at runtime though.

    Edit: As I thought, removing tiles from the tilemap at runtime has a cost of 0.
     
    Last edited: Oct 2, 2019