Search Unity

Any way to pre-generate entities and save them to load at runtime?

Discussion in 'Entity Component System' started by colin_young, Jan 8, 2020.

  1. colin_young

    colin_young

    Joined:
    Jun 1, 2017
    Posts:
    243
    I am generating entities at startup from lane information in EasyRoads3D (MonoBehaviours I believe). Ultimately I want to "finalize" the EasyRoads3D road network which will have the result of leaving only the meshes, so I'm going to need a way to preserve the lane data. Is there some way I can save the entity graph I generate and just load that directly at runtime? I kind of have the impression that the subscene workflow might be related or helpful here, but I'm not really sure where to start with that.
     
  2. siggigg

    siggigg

    Joined:
    Apr 11, 2018
    Posts:
    247
    Subscenes are perfect for that :) Watch this talk from Unite 2019

     
  3. desertGhost_

    desertGhost_

    Joined:
    Apr 12, 2018
    Posts:
    260
    This depends on how you store your graph.

    Let's say that your graph consists of entities placed in world space with two components attached:

    Code (CSharp):
    1. public struct Vertice : IComponentData
    2. {
    3.     public int value;
    4. }
    5.  
    6. public struct Edge : IBufferElementData
    7. {
    8.     public int vertice;
    9.     public Entity verticeEntity;
    10.     public int cost;
    11. }
    12.  
    Lets say you have an authoring component
    GraphNodeAuthoring
    that adds these components with the proper data to each node entity.

    You can write an editor utility which bakes the graph data from the EasyRoads3D road network into a set of GameObjects. Each gameobject would be placed in the world space location of the node and have the
    GraphNodeAuthoring
    attached. Your editor utility will fill each
    GraphNodeAuthoring
    with the appropriate data for that node.

    Now that you have represented your EasyRoads3D road network with a set of gameobjects you can simple copy and paste those gameobjects into your subscene. If you want to automatically create a subscene in your editor utility, then you can look in SubSceneContextMenu.cs and SubSceneInspectorUtility.cs for code which will help you do so.
     
  4. colin_young

    colin_young

    Joined:
    Jun 1, 2017
    Posts:
    243
    @siggigg I will rewatch that video. Last time I knew much less about working with ECS so I expect I ignored a a lot of details.

    @desertGhost_ I will look at the idea of creating authoring components. I'm currently generating entities and components directly. This idea would have made things way easier to debug and it will make it more obvious when the generation has been done. I've already got a utility that half-bakes it, so it should be a relatively small change.

    Thanks!
     
  5. colin_young

    colin_young

    Joined:
    Jun 1, 2017
    Posts:
    243
    I just realized one issue I don't know how to solve: How do I reference one entity from another when I'm doing the conversion, especially when the second entity hasn't been converted yet? i.e. A depends on B and B depends on A, in other words my graph is cyclic.
     
  6. desertGhost_

    desertGhost_

    Joined:
    Apr 12, 2018
    Posts:
    260
    I think using
    conversionSystem.GetPrimaryEntity(whateverYourNeighborNodeGameObjectIs)
    will get you the entity that you need.
     
    colin_young likes this.
  7. siggigg

    siggigg

    Joined:
    Apr 11, 2018
    Posts:
    247
    If you do have a gameobject-based hierarchy that you are converting, you can set up entity-to-entity references just like you did gameobject ones before.

    If you are using authoring components, simply create a GameObject field on the monobehavior or an Entity type field if you are using auto-generated authoring components. Then in your GO graph simply drag the other objects from the hierarchy to that field on the inspector. At runtime this will then have the Entity that was generated from the referenced GO.
     
    OwlchemyDawson and colin_young like this.
  8. colin_young

    colin_young

    Joined:
    Jun 1, 2017
    Posts:
    243
    For the record, this worked perfectly. Thanks!
     
    desertGhost_ likes this.