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?
using Unity.Collections;
using Unity.Entities;
using System;
using UnityEngine;
using Unity.Mathematics;
namespace Lightbringer.HexWorld
{
/// <summary>
/// This class is what builds the entity cache through sub scenes.
/// It converts hexSphera vertex and triangle data to entity data.
/// </summary>
[UpdateAfter(typeof(HexSphereBuildDataConversion))]
public class HexSphereBuildConversionSystem : GameObjectConversionSystem
{
#region FLIELDS --------------------------------------------------------
private GameObject go;
private HexSphereBuildDataProxy data;
internal WORLD_SIZE worldSize;
private int subdivisionsCount;
private int raduis;
private int vertexCount;
private int triangleCount;
private EntityManager em;
private EntityArchetype arVertex;
private EntityArchetype arTriangle;
private EntityQuery qVertexComp;
private EntityQuery qTriangleComp;
#endregion
#region METHODS --------------------------------------------------------
protected override void OnUpdate()
{
Debug.Log("1");
//
// Get data
go = GameObject.FindGameObjectWithTag("tagHexSphereBuildGameObject");
data = go.GetComponent<HexSphereBuildDataProxy>();
worldSize = data.WorldSize;
subdivisionsCount = data.SubdivisionsCount;
raduis = data.Raduis;
vertexCount = data.VertexCount;
triangleCount = data.TriangleCount;
//em = World.Active.EntityManager; TODO : TESTING REPLACING WITH DstEntityManager
//var e = GetPrimaryEntity(go);//TEST
//
// First destroy existing entities
var qTileComp = new EntityQueryDesc
{
Any = new ComponentType[] { ComponentType.ReadOnly<TileTriangleComponent>(), ComponentType.ReadOnly<TileVertexComponent>() },
None = Array.Empty<ComponentType>(),
All = Array.Empty<ComponentType>(),
};
DstEntityManager.DestroyEntity(DstEntityManager.CreateEntityQuery(qTileComp));
var qVertTri = new EntityQueryDesc
{
Any = new ComponentType[] { ComponentType.ReadOnly<VertexComponent>(), ComponentType.ReadOnly<TriangleComponent>() },
None = Array.Empty<ComponentType>(),
All = Array.Empty<ComponentType>(),
};
DstEntityManager.DestroyEntity(DstEntityManager.CreateEntityQuery(qVertTri));
//
// Create Persistent data
HexSphereBuildData.Verticies = new NativeArray<VertexComponent>(vertexCount, Allocator.Persistent);
HexSphereBuildData.Triangles = new NativeArray<TriangleComponent>(triangleCount, Allocator.Persistent);
//
// Build data
HexSphereBuildUtils.BuildHexSphereSystem(subdivisionsCount, raduis, ref HexSphereBuildData.Verticies, ref HexSphereBuildData.Triangles);
////
//// Create Entity Archetype instantiation
//arVertex = DstEntityManager.CreateArchetype(typeof(VertexComponent), typeof(TileVertexComponent));
//arTriangle = DstEntityManager.CreateArchetype(typeof(TriangleComponent), typeof(TileTriangleComponent));
////
//// Batch create entities
//NativeArray<Entity> eVertces = new NativeArray<Entity>(vertexCount, Allocator.TempJob);
//NativeArray<Entity> enTriangles = new NativeArray<Entity>(triangleCount, Allocator.TempJob);
//DstEntityManager.CreateEntity(arVertex, eVertces);
//DstEntityManager.CreateEntity(arTriangle, enTriangles);
////
//// Create Entity EntityQuery
//qVertexComp = DstEntityManager.CreateEntityQuery(ComponentType.ReadOnly<VertexComponent>());
//qTriangleComp = DstEntityManager.CreateEntityQuery(ComponentType.ReadOnly<TriangleComponent>());
////
//// Batch Set entity component data
//qVertexComp.CopyFromComponentDataArray<VertexComponent>(HexSphereBuildData.Verticies);
//qTriangleComp.CopyFromComponentDataArray<TriangleComponent>(HexSphereBuildData.Triangles);
//TEST
for (var i = 0; i < vertexCount; i++)
{
var e = CreateAdditionalEntity(go);
//DstEntityManager.AddComponentData(e, HexSphereBuildData.Verticies[i]);
var vertexData = new VertexComponent
{
PositionX = HexSphereBuildData.Verticies[i].PositionX,
PositionY = HexSphereBuildData.Verticies[i].PositionY,
PositionZ = HexSphereBuildData.Verticies[i].PositionZ,
ConnectedTriA = HexSphereBuildData.Verticies[i].ConnectedTriA,
ConnectedTriB = HexSphereBuildData.Verticies[i].ConnectedTriB,
ConnectedTriC = HexSphereBuildData.Verticies[i].ConnectedTriC,
ConnectedTriD = HexSphereBuildData.Verticies[i].ConnectedTriD,
ConnectedTriE = HexSphereBuildData.Verticies[i].ConnectedTriE,
ConnectedTriF = HexSphereBuildData.Verticies[i].ConnectedTriF
};
this.DstEntityManager.AddComponentData(e, vertexData);
var vertexTileData = new TileVertexComponent
{
Position = new float3(0,0,0),
ConnectedTriA = Entity.Null,
ConnectedTriB = Entity.Null,
ConnectedTriC = Entity.Null,
ConnectedTriD = Entity.Null,
ConnectedTriE = Entity.Null,
ConnectedTriF = Entity.Null
};
this.DstEntityManager.AddComponentData(e, vertexTileData);
}
for (var i = 0; i < triangleCount; i++)
{
var e = CreateAdditionalEntity(go);
//DstEntityManager.AddComponentData(e, HexSphereBuildData.Verticies[i]);
var triangleData = new TriangleComponent
{
VertA = HexSphereBuildData.Triangles[i].VertA,
VertB = HexSphereBuildData.Triangles[i].VertB,
VertC = HexSphereBuildData.Triangles[i].VertC,
CenterVertex = HexSphereBuildData.Triangles[i].CenterVertex,
NeighborsAccrossAB = HexSphereBuildData.Triangles[i].NeighborsAccrossAB,
NeighborsAccrossBC = HexSphereBuildData.Triangles[i].NeighborsAccrossBC,
NeighborsAccrossCA = HexSphereBuildData.Triangles[i].NeighborsAccrossCA,
CenterPosition = HexSphereBuildData.Triangles[i].CenterPosition
};
this.DstEntityManager.AddComponentData(e, triangleData);
var tileTriangleData = new TileTriangleComponent
{
VertA = Entity.Null,
VertB = Entity.Null,
VertC = Entity.Null,
NeighborsAccrossAB = Entity.Null,
NeighborsAccrossBC = Entity.Null,
NeighborsAccrossCA = Entity.Null,
CenterPosition = new float3(0, 0, 0)
};
this.DstEntityManager.AddComponentData(e, tileTriangleData);
}
//
// Cleanup
//eVertces.Dispose();
// enTriangles.Dispose();
HexSphereBuildData.Verticies.Dispose();
HexSphereBuildData.Triangles.Dispose();
}
#endregion
}
}