Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Unity.Entities.Serialization Examples & Documentation?

Discussion in 'Entity Component System' started by MNNoxMortem, Jan 11, 2020.

  1. MNNoxMortem

    MNNoxMortem

    Joined:
    Sep 11, 2016
    Posts:
    723
    This thread is the generalization follow-up based on the answers in Most efficient way to load/create Raycast targets (e.g. Colliders) with DOTS?

    Do we have any documentation on the current state of how to use Unity.Entities.Serialization? Some very wonderful people posted examples in the linked thread on how to de/serialize Colliders created via
    Unity.Physics.MeshCollider.Create on how to really fast de/serialize results of expensive calculations (e.g. MeshCollider creation).

    As there had been quite some important things to be aware of (e.g. proper usage of ref) to get the example to work, I wonder if we have more information somewhere available. In particular what types are supported by Unity.Entities.Serialization (e.g. everything blittable?) and which of most common Unity build-in types are supported:
    • Texture, Texture2D, ...
    • Aabb, Bounds, ...
    • Material, ...
    • Mesh, ...
    • Arrays (which ones ?)
    • structs which contain arrays?
    I am aware of this documentation of the package which exposes the API but that is as much information I could find.
     
    Last edited: Jan 12, 2020
    JakHussain and thelebaron like this.
  2. JakHussain

    JakHussain

    Joined:
    Oct 20, 2016
    Posts:
    318
    I'd also be really interested in some more info on this too. I've messed around with blob assets but I want to start writing them into files. And reading from those files.
     
  3. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    Just have to dig through the Entities source BinarySerialization.cs and Blobs.cs have most of what you need. There are extensions that handle reading and writing native arrays and blobs. So the tools are all there mostly.

    Cut and paste of the core of our code for serializing blobs, we just standardize on arrays of them.

    Code (csharp):
    1.  
    2. public static unsafe void ReadBlobArray<T>(NativeList<BlobAssetReference<T>> blobs, string filename) where T : struct
    3.         {
    4.             EnsureTypePath<T>();
    5.             string path = BlobPath<T>(filename);
    6.  
    7.             using (StreamBinaryReader reader = new StreamBinaryReader(path))
    8.             {
    9.                 int count = reader.ReadInt();
    10.                 for(int i=0;i<count;i++)
    11.                 {
    12.                     var blob = reader.Read<T>();
    13.                     blobs.Add(blob);
    14.                 }
    15.             }
    16.         }
    17.         public static unsafe void WriteBlobArray<T>(NativeArray<BlobAssetReference<T>> blobs, string filename) where T : struct
    18.         {
    19.             EnsureTypePath<T>();
    20.             string path = BlobPath<T>(filename);
    21.  
    22.             using (StreamBinaryWriter writer = new StreamBinaryWriter(path))
    23.             {
    24.                 writer.Write(blobs.Length);
    25.                 for (int i = 0; i < blobs.Length; i++)
    26.                 {
    27.                     var blob = blobs[i];
    28.                     writer.Write(blob);
    29.                 }
    30.             }
    31.         }
    32.  
     
    MNNoxMortem and siggigg like this.