Search Unity

SubScenes Help with Live Link

Discussion in 'Entity Component System' started by RoughSpaghetti3211, Jun 23, 2019.

  1. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,705
    Ok Been trying for past 3 days to get subScenes working , trolled through MegaCity and the HelloCube subscens example but im just not getting this.

    Reason I want to used it is for the LiveLink. Can someone please walk me through the process on how to set this up. It seems I need small detailed steps, like a child flip book with pop up animals :(

    Thanks in advance.
     
  2. Spy-Shifty

    Spy-Shifty

    Joined:
    May 5, 2011
    Posts:
    546
    Create SubScene from current scene:
    • Add a "scene object" and attach all gameobjects that belongs to that scene to it.
    • Select the sceneobject and right click
    • Click on "New SubScene From Selection"
    upload_2019-6-23_22-48-26.png


    Click "Rebuild Entity Cache" everytime you change something on conversion scripts or prefabs that belongs to the SubScene.
    upload_2019-6-23_22-51-17.png


    Click close to create entities. All conversion scripts will be excuted for all gameobjects which belongs to the SubScene.
    upload_2019-6-23_22-52-17.png

    You are done. You should see somthing like this:
    upload_2019-6-23_22-53-8.png
     
  3. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,705
    Thank you so much, ok seem like my problem was not hitting the "close" button. Collaborate destroyed my project, once im up and running I will test it

    Thanks again !!
     
  4. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,705
    Ive gotten quite far with sub scenes and live like but Ive run into an unexpected behaviour. Let me explain.

    I have a subScene with two game objects for conversion to entities utilizing live link.

    Screen Shot 2019-08-18 at 3.05.04 PM.png

    I have two classes implementing GameObjectConversionSystem for each gameObject and one addition class where I want to add addition entites relating to the data from the two entity gameObjects

    For GameObject 1 Screen Shot 2019-08-18 at 3.10.57 PM.png
    For GameObject 2
    Screen Shot 2019-08-18 at 3.10.45 PM.png
    and the additional conversion class that add additional entities based on data from the 2 gameobjects.
    Screen Shot 2019-08-18 at 3.10.30 PM.png

    The main motivation for this is HexSphereBuildConversionSystem is expensive so I wanted to cache the entities in the editor. Live link is needed to get required data from gameObjects (gameObject 1 and gameObject 2).

    Seems to work like expected, I press "ReBuild Entity Cache" and I see gameObject 1 and 2 entities and my additional entities from HexSphereBuildConversionSystem.

    Screen Shot 2019-08-18 at 3.22.20 PM.png

    Now if I run the game it appears that my custom entities are lost.

    If enter playmode with the subScenes open by pressing . "Edit" my entities make it all the way through.

    Please help me understand what is going on here :(
     
  5. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,705
    Here is what my entity debugger looks like in play mode. Missing all my custom entities

    Screen Shot 2019-08-18 at 3.24.09 PM.png
     
  6. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,705
    Wondering if i need to tag it with some component so it doesn't get deleted
     
  7. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,759
    We have no idea what your code is doing
     
  8. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,705
    Basically tying to create and cache entities with no game object using live link. The first two conversions work but it this last one that is not working.

    I trying to pay the cost of building these entities in the editor not at run time.

    EDIT forgot code

    92 entities in the scene 'Assets/Scripts/HexWorld/HexSphere/HexSphere.Scenes/HexSphereBuildSubScene.unity' had no SceneSection and as a result were not serialized at all.

    Code (CSharp):
    1. using Unity.Collections;
    2. using Unity.Entities;
    3. using System;
    4. using UnityEngine;
    5.  
    6.  
    7. namespace Lightbringer.HexWorld
    8. {
    9.     /// <summary>
    10.     /// This class is what builds the entity cache through sub scenes.
    11.     /// It converts hexSphera vertex and triangle data to entity data.
    12.     /// </summary>
    13.     [UpdateAfter(typeof(HexSphereBuildDataConversion))]
    14.     public class HexSphereBuildConversionSystem : GameObjectConversionSystem
    15.     {
    16.         #region FLIELDS --------------------------------------------------------
    17.  
    18.         private GameObject go;
    19.         private HexSphereBuildDataProxy data;
    20.         internal WORLD_SIZE worldSize;
    21.         private int subdivisionsCount;
    22.         private int raduis;
    23.         private int vertexCount;
    24.         private int triangleCount;
    25.         private EntityManager em;
    26.         private EntityArchetype arVertex;
    27.         private EntityArchetype arTriangle;
    28.         private EntityQuery qVertexComp;
    29.         private EntityQuery qTriangleComp;
    30.  
    31.         #endregion
    32.  
    33.         #region METHODS --------------------------------------------------------
    34.  
    35.         protected override void OnUpdate()
    36.         {
    37.             Debug.Log("1");
    38.             //
    39.             // Get data
    40.             go = GameObject.FindGameObjectWithTag("tagHexSphereBuildGameObject");
    41.             data = go.GetComponent<HexSphereBuildDataProxy>();
    42.             worldSize = data.WorldSize;
    43.             subdivisionsCount = data.SubdivisionsCount;
    44.             raduis = data.Raduis;
    45.             vertexCount = data.VertexCount;
    46.             triangleCount = data.TriangleCount;
    47.             //em = World.Active.EntityManager; TODO : TESTING REPLACING WITH DstEntityManager
    48.  
    49.             //
    50.             // First destroy existing entities
    51.             var qTileComp = new EntityQueryDesc
    52.             {
    53.                 Any = new ComponentType[] { ComponentType.ReadOnly<TileTriangleComponent>(), ComponentType.ReadOnly<TileVertexComponent>() },
    54.                 None = Array.Empty<ComponentType>(),
    55.                 All = Array.Empty<ComponentType>(),
    56.             };
    57.             DstEntityManager.DestroyEntity(DstEntityManager.CreateEntityQuery(qTileComp));
    58.  
    59.             var qVertTri = new EntityQueryDesc
    60.             {
    61.                 Any = new ComponentType[] { ComponentType.ReadOnly<VertexComponent>(), ComponentType.ReadOnly<TriangleComponent>() },
    62.                 None = Array.Empty<ComponentType>(),
    63.                 All = Array.Empty<ComponentType>(),
    64.             };
    65.             DstEntityManager.DestroyEntity(DstEntityManager.CreateEntityQuery(qVertTri));
    66.  
    67.             //
    68.             // Create Persistent data
    69.             HexSphereBuildData.Verticies = new NativeArray<VertexComponent>(vertexCount, Allocator.Persistent);
    70.             HexSphereBuildData.Triangles = new NativeArray<TriangleComponent>(triangleCount, Allocator.Persistent);
    71.  
    72.             //
    73.             // Build data
    74.             HexSphereBuildUtils.BuildHexSphereSystem(subdivisionsCount, raduis, ref HexSphereBuildData.Verticies, ref HexSphereBuildData.Triangles);
    75.  
    76.             //
    77.             // Create Entity Archetype instantiation
    78.             arVertex = DstEntityManager.CreateArchetype(typeof(VertexComponent), typeof(TileVertexComponent));
    79.             arTriangle = DstEntityManager.CreateArchetype(typeof(TriangleComponent), typeof(TileTriangleComponent));
    80.  
    81.             //
    82.             // Batch create entities
    83.             NativeArray<Entity> eVertces = new NativeArray<Entity>(vertexCount, Allocator.TempJob);
    84.             NativeArray<Entity> enTriangles = new NativeArray<Entity>(triangleCount, Allocator.TempJob);
    85.             DstEntityManager.CreateEntity(arVertex, eVertces);
    86.             DstEntityManager.CreateEntity(arTriangle, enTriangles);
    87.  
    88.             //
    89.             // Create Entity EntityQuery
    90.             qVertexComp = DstEntityManager.CreateEntityQuery(ComponentType.ReadOnly<VertexComponent>());
    91.             qTriangleComp = DstEntityManager.CreateEntityQuery(ComponentType.ReadOnly<TriangleComponent>());
    92.  
    93.             //
    94.             // Batch Set entity component data
    95.             qVertexComp.CopyFromComponentDataArray<VertexComponent>(HexSphereBuildData.Verticies);
    96.             qTriangleComp.CopyFromComponentDataArray<TriangleComponent>(HexSphereBuildData.Triangles);
    97.  
    98.             //
    99.             // Cleanup
    100.             eVertces.Dispose();
    101.             enTriangles.Dispose();
    102.             HexSphereBuildData.Verticies.Dispose();
    103.             HexSphereBuildData.Triangles.Dispose();
    104.  
    105.         }
    106.  
    107.         #endregion
    108.     }
    109. }
    110.  
     
    Last edited: Aug 19, 2019
  9. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,759
    I haven't used subscenes in a while, but I suspect simply creating some entities in a system is not going to cause your entities to be serialized.
     
  10. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Newly created entities in the Destination world need to be associated with the source game object.
    So you need to use:

    See
    var entity = GetPrimaryEntity(meshRenderer);
    or
    var meshEntity = CreateAdditionalEntity(meshRenderer);

    MeshRendererConversion.cs as an example
    To create the entity, then you can add any component data to it you like.
     
    forteller likes this.
  11. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,705
    I see, thanks for the help
     
  12. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,705
    OK So I got it all serializing correctly with the script below but it is running so soooo much slower. Is there a way to create additional entities in batch like I had and associated with the source game object?

    Code (CSharp):
    1. using Unity.Collections;
    2. using Unity.Entities;
    3. using System;
    4. using UnityEngine;
    5. using Unity.Mathematics;
    6.  
    7.  
    8. namespace Lightbringer.HexWorld
    9. {
    10.     /// <summary>
    11.     /// This class is what builds the entity cache through sub scenes.
    12.     /// It converts hexSphera vertex and triangle data to entity data.
    13.     /// </summary>
    14.     [UpdateAfter(typeof(HexSphereBuildDataConversion))]
    15.     public class HexSphereBuildConversionSystem : GameObjectConversionSystem
    16.     {
    17.         #region FLIELDS --------------------------------------------------------
    18.  
    19.         private GameObject go;
    20.         private HexSphereBuildDataProxy data;
    21.         internal WORLD_SIZE worldSize;
    22.         private int subdivisionsCount;
    23.         private int raduis;
    24.         private int vertexCount;
    25.         private int triangleCount;
    26.         private EntityManager em;
    27.         private EntityArchetype arVertex;
    28.         private EntityArchetype arTriangle;
    29.         private EntityQuery qVertexComp;
    30.         private EntityQuery qTriangleComp;
    31.  
    32.         #endregion
    33.  
    34.         #region METHODS --------------------------------------------------------
    35.  
    36.         protected override void OnUpdate()
    37.         {
    38.             Debug.Log("1");
    39.             //
    40.             // Get data
    41.             go = GameObject.FindGameObjectWithTag("tagHexSphereBuildGameObject");
    42.             data = go.GetComponent<HexSphereBuildDataProxy>();
    43.             worldSize = data.WorldSize;
    44.             subdivisionsCount = data.SubdivisionsCount;
    45.             raduis = data.Raduis;
    46.             vertexCount = data.VertexCount;
    47.             triangleCount = data.TriangleCount;
    48.             //em = World.Active.EntityManager; TODO : TESTING REPLACING WITH DstEntityManager
    49.             //var e = GetPrimaryEntity(go);//TEST
    50.  
    51.             //
    52.             // First destroy existing entities
    53.             var qTileComp = new EntityQueryDesc
    54.             {
    55.                 Any = new ComponentType[] { ComponentType.ReadOnly<TileTriangleComponent>(), ComponentType.ReadOnly<TileVertexComponent>() },
    56.                 None = Array.Empty<ComponentType>(),
    57.                 All = Array.Empty<ComponentType>(),
    58.             };
    59.             DstEntityManager.DestroyEntity(DstEntityManager.CreateEntityQuery(qTileComp));
    60.  
    61.             var qVertTri = new EntityQueryDesc
    62.             {
    63.                 Any = new ComponentType[] { ComponentType.ReadOnly<VertexComponent>(), ComponentType.ReadOnly<TriangleComponent>() },
    64.                 None = Array.Empty<ComponentType>(),
    65.                 All = Array.Empty<ComponentType>(),
    66.             };
    67.             DstEntityManager.DestroyEntity(DstEntityManager.CreateEntityQuery(qVertTri));
    68.  
    69.             //
    70.             // Create Persistent data
    71.             HexSphereBuildData.Verticies = new NativeArray<VertexComponent>(vertexCount, Allocator.Persistent);
    72.             HexSphereBuildData.Triangles = new NativeArray<TriangleComponent>(triangleCount, Allocator.Persistent);
    73.  
    74.             //
    75.             // Build data
    76.             HexSphereBuildUtils.BuildHexSphereSystem(subdivisionsCount, raduis, ref HexSphereBuildData.Verticies, ref HexSphereBuildData.Triangles);
    77.  
    78.             ////
    79.             //// Create Entity Archetype instantiation
    80.             //arVertex = DstEntityManager.CreateArchetype(typeof(VertexComponent), typeof(TileVertexComponent));
    81.             //arTriangle = DstEntityManager.CreateArchetype(typeof(TriangleComponent), typeof(TileTriangleComponent));
    82.  
    83.             ////
    84.             //// Batch create entities
    85.             //NativeArray<Entity> eVertces = new NativeArray<Entity>(vertexCount, Allocator.TempJob);
    86.             //NativeArray<Entity> enTriangles = new NativeArray<Entity>(triangleCount, Allocator.TempJob);
    87.             //DstEntityManager.CreateEntity(arVertex, eVertces);
    88.             //DstEntityManager.CreateEntity(arTriangle, enTriangles);
    89.  
    90.             ////
    91.             //// Create Entity EntityQuery
    92.             //qVertexComp = DstEntityManager.CreateEntityQuery(ComponentType.ReadOnly<VertexComponent>());
    93.             //qTriangleComp = DstEntityManager.CreateEntityQuery(ComponentType.ReadOnly<TriangleComponent>());
    94.  
    95.             ////
    96.             //// Batch Set entity component data
    97.             //qVertexComp.CopyFromComponentDataArray<VertexComponent>(HexSphereBuildData.Verticies);
    98.             //qTriangleComp.CopyFromComponentDataArray<TriangleComponent>(HexSphereBuildData.Triangles);
    99.  
    100.  
    101.             //TEST
    102.             for (var i = 0; i < vertexCount; i++)
    103.             {
    104.                 var e = CreateAdditionalEntity(go);
    105.  
    106.                 //DstEntityManager.AddComponentData(e, HexSphereBuildData.Verticies[i]);
    107.  
    108.                 var vertexData = new VertexComponent
    109.                 {
    110.                     PositionX = HexSphereBuildData.Verticies[i].PositionX,
    111.                     PositionY = HexSphereBuildData.Verticies[i].PositionY,
    112.                     PositionZ = HexSphereBuildData.Verticies[i].PositionZ,
    113.                     ConnectedTriA = HexSphereBuildData.Verticies[i].ConnectedTriA,
    114.                     ConnectedTriB = HexSphereBuildData.Verticies[i].ConnectedTriB,
    115.                     ConnectedTriC = HexSphereBuildData.Verticies[i].ConnectedTriC,
    116.                     ConnectedTriD = HexSphereBuildData.Verticies[i].ConnectedTriD,
    117.                     ConnectedTriE = HexSphereBuildData.Verticies[i].ConnectedTriE,
    118.                     ConnectedTriF = HexSphereBuildData.Verticies[i].ConnectedTriF
    119.                 };
    120.  
    121.                 this.DstEntityManager.AddComponentData(e, vertexData);
    122.  
    123.                 var vertexTileData = new TileVertexComponent
    124.                 {
    125.                     Position = new float3(0,0,0),
    126.                     ConnectedTriA = Entity.Null,
    127.                     ConnectedTriB = Entity.Null,
    128.                     ConnectedTriC = Entity.Null,
    129.                     ConnectedTriD = Entity.Null,
    130.                     ConnectedTriE = Entity.Null,
    131.                     ConnectedTriF = Entity.Null
    132.                 };
    133.  
    134.                 this.DstEntityManager.AddComponentData(e, vertexTileData);
    135.             }
    136.  
    137.             for (var i = 0; i < triangleCount; i++)
    138.             {
    139.                 var e = CreateAdditionalEntity(go);
    140.  
    141.                 //DstEntityManager.AddComponentData(e, HexSphereBuildData.Verticies[i]);
    142.  
    143.                 var triangleData = new TriangleComponent
    144.                 {
    145.                     VertA = HexSphereBuildData.Triangles[i].VertA,
    146.                     VertB = HexSphereBuildData.Triangles[i].VertB,
    147.                     VertC = HexSphereBuildData.Triangles[i].VertC,
    148.                     CenterVertex = HexSphereBuildData.Triangles[i].CenterVertex,
    149.                     NeighborsAccrossAB = HexSphereBuildData.Triangles[i].NeighborsAccrossAB,
    150.                     NeighborsAccrossBC = HexSphereBuildData.Triangles[i].NeighborsAccrossBC,
    151.                     NeighborsAccrossCA = HexSphereBuildData.Triangles[i].NeighborsAccrossCA,
    152.                     CenterPosition = HexSphereBuildData.Triangles[i].CenterPosition
    153.                 };
    154.  
    155.                 this.DstEntityManager.AddComponentData(e, triangleData);
    156.  
    157.  
    158.                 var tileTriangleData = new TileTriangleComponent
    159.                 {
    160.                     VertA = Entity.Null,
    161.                     VertB = Entity.Null,
    162.                     VertC = Entity.Null,
    163.                     NeighborsAccrossAB = Entity.Null,
    164.                     NeighborsAccrossBC = Entity.Null,
    165.                     NeighborsAccrossCA = Entity.Null,
    166.                     CenterPosition = new float3(0, 0, 0)
    167.                 };
    168.  
    169.                 this.DstEntityManager.AddComponentData(e, tileTriangleData);
    170.  
    171.  
    172.             }
    173.  
    174.  
    175.  
    176.             //
    177.             // Cleanup
    178.             //eVertces.Dispose();
    179.             //         enTriangles.Dispose();
    180.             HexSphereBuildData.Verticies.Dispose();
    181.             HexSphereBuildData.Triangles.Dispose();
    182.  
    183.         }
    184.  
    185.         #endregion
    186.     }
    187. }
    188.  
     
  13. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,705
    Wondering if someone could help me understand why my GameObjectConversionSystem run twice when I enter "Edit" mode and "Rebuild Entity Cache".
     
    Last edited: Aug 24, 2019
  14. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    You are definitely doing this wrong... Please follow MeshRendererConversion.cs as an example.

    * GameObject.FindGameObjectWithTag being used
    * There being state on the system which is dependent on the data in the scene
    Points to the code being incorrect.
     
  15. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,705
    I started rewriting this looking at MeshRenderConversion.cs but Im confused. So what Im tying to achieve is convert a GameObject with Component of type to entities and add addition entities with additional component data not on the GameObject. Pretty much exactly what MeshRenderConversion.cs is doing but what im not understanding is this:

    1)
    MeshRenderConversion.cs has
    Code (CSharp):
    1.            
    2. Entities.ForEach((MeshRenderer meshRenderer, MeshFilter meshFilter) =>
    3.  {
    4. ...
    5. }
    6.  
    But where are the entities created ?

    2) My additional entities does not belong to any gameObject they are just data. What should pass to GetPrimaryEntity(?) and how do I get a gameObject to associate if I should not be using FindGameObjectWithTag
    Code (CSharp):
    1. var entity = GetPrimaryEntity(meshRenderer);
    I think im missing a lot here but hop to fill the knowledge gap
     
  16. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,705
    OK took a step back and tried to write a simple GameObjectConversionSystem. I know this doesnt work but hoping someone can take a looks and teach me.

    Still using GameObject.FindGameObjectWithTag(), not sure how to get the GameObject the correct way to pass to GetPrimaryEntity.

    GetPrimaryEntity() returns a "NullReferenceException: Object reference not set to an instance of an object"

    This is as far as I got digging thought the examples

    Code (CSharp):
    1.  
    2.  
    3. namespace Lightbringer.HexWorld
    4. {
    5.     public class HexSphereBuildDataConversion : GameObjectConversionSystem
    6.     {
    7.             go = GameObject.FindGameObjectWithTag("tagHexSphereBuildGameObject");
    8.             data = go.GetComponent<HexSphereBuildDataProxy>();
    9.  
    10.             Entities.ForEach((HexSphereBuildDataProxy data) =>
    11.             {
    12.  
    13.                 var entity = GetPrimaryEntity(go);    
    14.                 //var entity = CreateAdditionalEntity(data);
    15.  
    16.                 var componentData = new HexSphereBuildDataComponent
    17.                 {
    18.                     WorldSize = data.WorldSize,
    19.                     SubdivisionsCount = data.SubdivisionsCount,
    20.                     Raduis = data.Raduis,
    21.                     VertexCount = data.VertexCount,
    22.                     TriangleCount = data.TriangleCount
    23.                 };
    24.  
    25.                DstEntityManager.AddComponentData(entity, componentData);
    26.                 Debug.Log(go.name + " //// TEST");
    27.             });
    28.  
    29.         }
    30.     }
    31. }
     
    Last edited: Aug 26, 2019
  17. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,759
    what is this?

    Code (CSharp):
    1. go = GameObject.FindGameObjectWithTag("tagHexSphereBuildGameObject");
    2. data = go.GetComponent<HexSphereBuildDataProxy>();
    3.  
    4. Entities.ForEach((HexSphereBuildDataProxy data) =>
    what are you attempting to do with the go/data doing before you do a entities foreach

    -edit-

    you're way over-complicating this. all you need is this

    Code (CSharp):
    1. public class HexSphereBuildDataConversion : GameObjectConversionSystem
    2. {
    3.     protected override void OnUpdate()
    4.     {
    5.         Entities.ForEach((HexSphereBuildDataProxy data) =>
    6.         {
    7.             var entity = GetPrimaryEntity(data);  
    8.         }
    9.     }
    10. }
     
    Last edited: Aug 26, 2019
  18. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,705
    It where I started, but I keep getting a null reference exception. I just dont understand how data can exist anywhere appart from on the gameObject. Am I suppose to implement a system prior to this creating the data?

    Edit : did a sanity check, just ran the code above praying it would work but still gettng a null reference exception on GetPrimaryEntity.
     
  19. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,759
    I just setup a scene and ran the code I posted and it works fine. I don't know what you're doing wrong.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class HexSphereBuildDataProxy  : MonoBehaviour
    4. {
    5.     public int ExtraEntities;
    6. }
    7.  
    8. public class HexSphereBuildDataConversion : GameObjectConversionSystem
    9. {
    10.     protected override void OnUpdate()
    11.     {
    12.         this.Entities.ForEach((HexSphereBuildDataProxy data) =>
    13.         {
    14.             var entity = this.GetPrimaryEntity(data);
    15.             Debug.Log(data.gameObject);
    16.             Debug.Log($"Primary: {entity}");
    17.  
    18.             for (var i = 0; i < data.ExtraEntities; i++)
    19.             {
    20.                 var extraEntity = this.CreateAdditionalEntity(data);
    21.                 Debug.Log($"Extra: {extraEntity}");
    22.             }
    23.         });
    24.     }
    25. }
    26.  
    MainScene:

    upload_2019-8-26_14-55-54.png

    SubScene:

    upload_2019-8-26_14-55-22.png

    Result:

    upload_2019-8-26_14-55-9.png
     
  20. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,705
    What the hell , ok let me go through all the code again ... Thanks heaps for the example .. it give me hope
     
  21. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,705
    OK got it working, slow but working thanks again tertle.

    Code (CSharp):
    1. namespace Lightbringer.HexWorld
    2. {
    3.     /// <summary>
    4.     /// Convert game object with hexSphereBuildData to entities.
    5.     /// </summary>
    6.     //ToDo : [UpdateBefore(typeof(HexSphereBuildConversionSystem))]
    7.     public class HexSphereBuildDataConversion : GameObjectConversionSystem
    8.     {
    9.         #region FLIELDS --------------------------------------------------------
    10.  
    11.         #endregion
    12.  
    13.         #region METHODS --------------------------------------------------------
    14.  
    15.         protected override void OnUpdate()
    16.         {
    17.             this.Entities.ForEach((HexSphereBuildDataProxy data) =>
    18.             {
    19.                 Debug.Log("0");
    20.                 //
    21.                 // Create Persistent data
    22.                 HexSphereBuildData.Verticies = new NativeArray<VertexComponent>(data.VertexCount, Allocator.Persistent);
    23.                 HexSphereBuildData.Triangles = new NativeArray<TriangleComponent>(data.TriangleCount, Allocator.Persistent);
    24.  
    25.                 //
    26.                 // Build data
    27.                 HexSphereBuildUtils.BuildHexSphereSystem(data.SubdivisionsCount, data.Raduis, ref HexSphereBuildData.Verticies, ref HexSphereBuildData.Triangles);
    28.  
    29.                 //
    30.                 // Create primaty data entity
    31.                 var ePrimary = this.GetPrimaryEntity(data);
    32.  
    33.                 var cData = new HexSphereBuildDataComponent
    34.                 {
    35.                     WorldSize = data.WorldSize,
    36.                     SubdivisionsCount = data.SubdivisionsCount,
    37.                     Raduis = data.Raduis,
    38.                     VertexCount = data.VertexCount,
    39.                     TriangleCount = data.TriangleCount
    40.                 };
    41.  
    42.                 this.DstEntityManager.AddComponentData(ePrimary, cData);
    43.  
    44.                
    45.                 // ToDo: This is super slow because I cant batch create and
    46.                 // set additional entities with CreateAdditionalEntity
    47.  
    48.  
    49.                 //
    50.                 // Create additional vertex entities
    51.                 for (var i = 0; i < data.VertexCount; i++)
    52.                 {
    53.                     var extreExtra = this.CreateAdditionalEntity(data);
    54.  
    55.                     var cVertexData = new VertexComponent
    56.                     {
    57.                         PositionX = HexSphereBuildData.Verticies[i].PositionX,
    58.                         PositionY = HexSphereBuildData.Verticies[i].PositionY,
    59.                         PositionZ = HexSphereBuildData.Verticies[i].PositionZ,
    60.                         ConnectedTriA = HexSphereBuildData.Verticies[i].ConnectedTriA,
    61.                         ConnectedTriB = HexSphereBuildData.Verticies[i].ConnectedTriB,
    62.                         ConnectedTriC = HexSphereBuildData.Verticies[i].ConnectedTriC,
    63.                         ConnectedTriD = HexSphereBuildData.Verticies[i].ConnectedTriD,
    64.                         ConnectedTriE = HexSphereBuildData.Verticies[i].ConnectedTriE,
    65.                         ConnectedTriF = HexSphereBuildData.Verticies[i].ConnectedTriF
    66.                     };
    67.  
    68.                     this.DstEntityManager.AddComponentData(extreExtra, cVertexData);
    69.  
    70.                     var cVertexTileData = new TileVertexComponent
    71.                     {
    72.                         Position = new float3(0, 0, 0),
    73.                         ConnectedTriA = Entity.Null,
    74.                         ConnectedTriB = Entity.Null,
    75.                         ConnectedTriC = Entity.Null,
    76.                         ConnectedTriD = Entity.Null,
    77.                         ConnectedTriE = Entity.Null,
    78.                         ConnectedTriF = Entity.Null
    79.                     };
    80.  
    81.                     this.DstEntityManager.AddComponentData(extreExtra, cVertexTileData);
    82.                 }
    83.  
    84.                 //
    85.                 // Create additional triangle entities
    86.                 for (var i = 0; i < data.TriangleCount; i++)
    87.                 {
    88.                     var eExtra = this.CreateAdditionalEntity(data);
    89.  
    90.                     var cTriangleData = new TriangleComponent
    91.                     {
    92.                         VertA = HexSphereBuildData.Triangles[i].VertA,
    93.                         VertB = HexSphereBuildData.Triangles[i].VertB,
    94.                         VertC = HexSphereBuildData.Triangles[i].VertC,
    95.                         CenterVertex = HexSphereBuildData.Triangles[i].CenterVertex,
    96.                         NeighborsAccrossAB = HexSphereBuildData.Triangles[i].NeighborsAccrossAB,
    97.                         NeighborsAccrossBC = HexSphereBuildData.Triangles[i].NeighborsAccrossBC,
    98.                         NeighborsAccrossCA = HexSphereBuildData.Triangles[i].NeighborsAccrossCA,
    99.                         CenterPosition = HexSphereBuildData.Triangles[i].CenterPosition
    100.                     };
    101.  
    102.                     this.DstEntityManager.AddComponentData(eExtra, cTriangleData);
    103.  
    104.                     var tileTriangleData = new TileTriangleComponent
    105.                     {
    106.                         VertA = Entity.Null,
    107.                         VertB = Entity.Null,
    108.                         VertC = Entity.Null,
    109.                         NeighborsAccrossAB = Entity.Null,
    110.                         NeighborsAccrossBC = Entity.Null,
    111.                         NeighborsAccrossCA = Entity.Null,
    112.                         CenterPosition = new float3(0, 0, 0)
    113.                     };
    114.  
    115.                     this.DstEntityManager.AddComponentData(eExtra, tileTriangleData);
    116.                 }
    117.  
    118.                 //
    119.                 // Cleanup
    120.                 HexSphereBuildData.Verticies.Dispose();
    121.                 HexSphereBuildData.Triangles.Dispose();
    122.             });
    123.         }
    124.  
    125.         #endregion
    126.     }
    127. }
     
  22. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,759
    Are you really creating 1 entity per vertex? That seems pretty nutty.
     
  23. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,705
    Yes , the game is planet so you could think of it as the units. When I release the game in year 2100 it will make sense if we are still around. ;)

    Also why I’m trying to learn ESC .. no other way to make this game.
     
  24. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,705
    How do I ensure that the next system accessing these entities dont run while im reBuilding the entity cache. Currently when I enter play mode all is well. The next system is happily doing what it needs to with TriangleComponent .. ect .

    Then when I rebuild entities cache I get an IndexOutOfRangeException because GameObjectConversionSystem is still converting while my next system is running. I need some sort of barrier?
     
  25. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,759
    why are you rebuilding entity cache in play mode anyway
     
  26. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,705
    The subdivision changes at run time

    EDIT : it was just as a tests but got me wondering
     
    Last edited: Aug 27, 2019
  27. rsodre

    rsodre

    Joined:
    May 9, 2012
    Posts:
    229
    What if I need a pure empty Entity? (I have an entity buffer with generic parameters)

    If the only way to create a new Entity during SubScene conversion is by cloning the primary entity, it will contain the converted components that I don't need, incuding all the transform stuff.

    DstEntityManager.CreateEntity(); should take care of this and create a minimal entity in the SubScene.
     
    Last edited: Oct 22, 2019