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 DOTS Serialization Package

Discussion in 'Entity Component System' started by Hysparions, Oct 8, 2020.

  1. Hysparions

    Hysparions

    Joined:
    Jan 7, 2019
    Posts:
    29
    Hello everyone,

    Building a game using Dots architecture, I worked on the serialization of some entities component. What I wanted was to create a unique file where I store the Maps (Procedurally Generated & edited). I ended up on creating a System reading entities and for each components to save, convert them into bytes blocks and write them into a file using standard C# IO. This is not so bad in performance, but requires me to do a lot of code for each serialization case. Therefore I also noticed a new Enities.Serialization Packages which would certainly be better and suited for ECS architecture

    However, I couldn't find any example nor documentation on the usage of the package on the net… So if some of you, or maybe even Unity dev do know how to get these things work, feel free to help !
     
  2. WAYNGames

    WAYNGames

    Joined:
    Mar 16, 2019
    Posts:
    988
    Hi, there is this documentation.
    https://docs.unity3d.com/Packages/c....Entities.Serialization.SerializeUtility.html

    It seems it only allows to serialize/deserialze the entire world, so if you need only to serialize some entities, I would copy those to a separate "serialization" world and work from there. (be carefull about entity references in you serialized entities !)

    Maybe take a look at the net code package also, I did not play a lot with the new versions but I understood from the change log that the serialization was done automatically and did not need additional user code. Maybe you can reuse that to simplify your code ?
     
    Hysparions likes this.
  3. Hysparions

    Hysparions

    Joined:
    Jan 7, 2019
    Posts:
    29
    Thanks for the reply, I will try to do this trick while waiting for the serialization API to become better!
     
  4. jGate99

    jGate99

    Joined:
    Oct 22, 2013
    Posts:
    1,936
    So there is no way of serializing/deserializing a class with NativeList, NativeHasMap etc? Even Json.Net doesnt seem to work with this and im forced to make a seperate class with typical Dictionary and List deserailie it and then copy to Native counterparts

    Please advise
     
  5. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    NativeArray/List serialization is there you really can't miss it if you look at the package source, Entities/Serialization/BinarySerialization.

    Get used to browsing package sources if you use DOTS. There are quite a few gems that just don't have more then stub api docs.
     
    Hysparions and jGate99 like this.
  6. jGate99

    jGate99

    Joined:
    Oct 22, 2013
    Posts:
    1,936
    Thank you for your reply but what if i'm using Job System with native collections and not entities itself?
     
    tonytopper likes this.
  7. tonytopper

    tonytopper

    Joined:
    Jun 25, 2018
    Posts:
    225
    I too am using Jobs, therefore Native Containers, but not using entities, yet. Looking to serialize some NativeHashMap containers living in a Monobehaviour script. Since I do live coding, aka hot reloading, and prototyping a lot, I tend to use the ISerializationCallbackReceiver for things that don't support the SerializeField annotation.

    The way I currently know for doing this to set up some serializable containers to shuffle the data in and out of using OnBeforeSerialize() and OnAfterDeserialize().

    Unfortunately, Native Containers get deallocated before OnBeforeSerialize is called, even ones marked as Persistent.

    And I get some complaints about EditorPrefsGetInt not being allowed during serialization when I try to allocate memory for a NativeHashMap in OnAfterDeserialize.

    Now it looks like I am going to have to store the data in a non-Native Container as soon as the Job finishes rather than wait until it needs to be serialized, which would be bad since the Job might run 100 times on this Monobehaviour's data during its lifetime.

    Still in the architecture phase for this game and wondering if I should start looking at ScriptableObjects to hold this data or make the leap into the ECS world.
     
  8. pshtif

    pshtif

    Joined:
    Mar 6, 2017
    Posts:
    12
    Sorry for exavating an old post but this seems to be still an issue nowadays. There doesn't seem to be a more straightforward way to serialize/deserialize native containers if we don't use ECS at all.
     
  9. tonytopper

    tonytopper

    Joined:
    Jun 25, 2018
    Posts:
    225
    I never found a solution. Still copying to managed data after the job finishes. :(
     
  10. Deleted User

    Deleted User

    Guest

    Prodigga and WAYNGames like this.