Search Unity

How can I deal with datas in ECS?

Discussion in 'Data Oriented Technology Stack' started by cloud1989, Aug 21, 2019.

  1. cloud1989

    cloud1989

    Joined:
    May 30, 2016
    Posts:
    32
    Code (CSharp):
    1. public struct HexMeshData : IComponentData {
    2.     //public float3[] Vertices;ArgumentException: HexMeshData contains a field of UnityEngine.float3[], which is neither primitive nor blittable.
    3.     //public Vector3[] Vertices;//ArgumentException: HexMeshData contains a field of UnityEngine.Vector3[], which is neither primitive nor blittable.
    4.     public NativeList<float3> Vertices;//ArgumentException: HexMeshData contains a field of Unity.Collections.LowLevel.Unsafe.DisposeSentinel, which is neither primitive nor blittable.
    5. }
    I'm very confusing now,anyone can tell me which fields I can use?Thanks a lot!
     
  2. recursive

    recursive

    Joined:
    Jul 12, 2012
    Posts:
    591
    You can't put container types / Arrays / NativeArrays<T> inside of an IComponentData struct.

    For lists/arrays, see IBufferElementData & DynamicBuffer<T> for a component that acts as a list/array.

    For ECS-friendlier math types, there's the Unity.Mathematics library, which can take advantage of Burst optimizations in Bursted Jobs.
     
    cloud1989 likes this.
  3. cloud1989

    cloud1989

    Joined:
    May 30, 2016
    Posts:
    32
    Hey man,thank you very much!
     
  4. cloud1989

    cloud1989

    Joined:
    May 30, 2016
    Posts:
    32
    So what type should I use for NativeList<float3> or NativeList<Vector3>? Thanks a lot!
     
  5. PublicEnumE

    PublicEnumE

    Joined:
    Feb 3, 2019
    Posts:
    105
  6. cloud1989

    cloud1989

    Joined:
    May 30, 2016
    Posts:
    32
    I'm tring to develop a auto create map system which can create Infinite map.Wherever players go,there always a perfect new map for them.Like the magacity,but bigger than it,Player is alway in the center of the current little map,long before the player reach the edge of the map new mesh will be automatically created by the map system and send the data to the Server which updates the map database.If the Server map data changed,other players also be noticed.The system also automatically predict where the player may go,then create new mapmesh or update from the Server database if there're datas for the direction the player may go.
    So I want to use the ECS for this system,I think it would be awesome if I did this.The lists is for mesh vertices.
    If you can help me,please check this out:https://github.com/cloudhu/HexMapMadeInUnity2019ECS
    I really need some help,thank you very much!
     
  7. cloud1989

    cloud1989

    Joined:
    May 30, 2016
    Posts:
    32
    Code (CSharp):
    1. public struct HexMeshData : IBufferElementData {
    2.     // These implicit conversions are optional, but can help reduce typing.
    3.     public static implicit operator Vector3(HexMeshData e) { return e.Value; }
    4.     public static implicit operator HexMeshData(Vector3 e) { return new HexMeshData { Value = e }; }
    5.  
    6.     // Actual value each buffer element will store.
    7.     public Vector3 Value;
    8. }
    Hey man,how can I use this in a Job like this :
    Code (CSharp):
    1.     [BurstCompile]
    2.     private struct CopySimPointsToVerticesJob : IJobForEachWithEntity<Translation> {
    3.         public NativeList<Vector3> Vertices;
    4.         public NativeList<int> Triangles;
    5.         public void Execute(Entity entity, int index, [ReadOnly]ref Translation position)
    6.         {
    7.             var currentPosition = position.Value;
    8.  
    9.             Vector3 center = new Vector3
    10.             {
    11.                 x = currentPosition.x,
    12.                 y = currentPosition.y,
    13.                 z = currentPosition.z
    14.             };
    15.  
    16.             for (int i = 0; i < 6; i++)
    17.             {
    18.                 int verticesIndex = Vertices.Length;
    19.                 Vertices.Add(center);
    20.                 Vertices.Add(center + HexMetrics.corners[i]);
    21.                 Vertices.Add(center + HexMetrics.corners[i + 1]);
    22.                 Triangles.Add(verticesIndex);
    23.                 Triangles.Add(verticesIndex+1);
    24.                 Triangles.Add(verticesIndex+2);
    25.             }
    26.         }
    27.     }
    Looking forward to your reply,you're very kind and helpful,thanks a lot!
     
  8. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    423
    Either BufferFromEntity or IJobForEachWithEntity_EB
     
  9. recursive

    recursive

    Joined:
    Jul 12, 2012
    Posts:
    591
    I reccomend the new float3 type and math functions from mathematics, it has implicit conversions to Vector3 if you need it.
     
  10. cloud1989

    cloud1989

    Joined:
    May 30, 2016
    Posts:
    32
    Thanks everyone,problem fixed!
     
    recursive likes this.