Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

How to serialize/deserialize entities at runtime

Discussion in 'Entity Component System' started by Klusimo, Nov 21, 2020.

  1. Klusimo

    Klusimo

    Joined:
    May 7, 2019
    Posts:
    76
    Hello, I have project in which I have entities in "quadrants"(divided parts of world) and I want to serialize them and save them into file and deserialize and instantiate them at runtime when needed.

    I got tip of using SerializeUtilityHybrid, but I dont really understand it and if it is good.

    Next I thought of using NativeMultiHashMap to store the serialized data with each quadrant being a key in the hashmap and each serialized entity going into its quadrant key. I would then write this entire NativeMultiHashMap into a file and when loading I would read it and take needed entities from it and instantiating them back into the world in the exact same state as they were before.

    What do you think about these proposed methods, are they good? are they performant? are they reliable? And are there better?

    I am open and will be thankful for any tips, tricks, ideas and suggestions. Thank you very much.
     
  2. myxolobe

    myxolobe

    Joined:
    Dec 7, 2016
    Posts:
    9
    You might find this interesting: https://medium.com/@5argon/unity-ecs-serializing-ecs-data-252231e5b16d

    I think any tools available in DOTS are intended to be very fast and reliable - but it's still very new and the implementations will evolve over time. When it comes to topics of serialization, the performance often relies on the programmer's use of the tool rather than the tool itself.

    For example, a tool that enables very fast serialization may require the data be in a specific format, so then you're adding custom code to transform the data pre- and post-serialization, defeating the purpose. On the other hand, a tool that serializes your data in its native format may take more space and have more overhead, but it requires less planning, engineering, maintenance, headaches, etc. The difference in speed is probably subtle enough to justify using the easier option.

    Not to mention backwards compatibility - what if your save data becomes incompatible after upgrading your game version? This can be an issue regardless of serialization protocol, and requires careful planning.
     
  3. MNNoxMortem

    MNNoxMortem

    Joined:
    Sep 11, 2016
    Posts:
    723
    And the most important part is that only ComponentData can be serialized with Dots, all non-dots data (classes, Component, MonoBehaviour) e.g. Mesh have to be re-loaded/constructed.
     
  4. myxolobe

    myxolobe

    Joined:
    Dec 7, 2016
    Posts:
    9
    Hm. I wonder if that could be avoided with Addressables or ScriptableObjects. At least for static asset references?
     
  5. Klusimo

    Klusimo

    Joined:
    May 7, 2019
    Posts:
    76
    I know DOTS is still in production and that it will change, I just want to know if there are any tools already, I am not much informed in this field.
     
  6. MNNoxMortem

    MNNoxMortem

    Joined:
    Sep 11, 2016
    Posts:
    723
    That would not avoid anything, as all three are very different tools and you can combine them as you wish.

    An example what you cannot do (at least not the last time I checked and got an answer from the Unity team here on the forum):
    * You cannot create a mesh at runtime, and serialize the Entities world, including the mesh.

    An example of what you can do:
    * You can serialize the World that has Entities with a mesh, but they will miss the mesh once you deserialiez them, therefore you need to re-assign the mesh after deserialization (e.g. loading it from addressables, re-creating it, or any other way)
    * You can serialize all the data, including the data that define the mesh (positions, indices) with the tools that the Entities package provide you. You will still need to re-create the mesh after you have loaded it again.
    * You can use addressables and/or scriptable objects to load data at runtime, to augment load data into your Entities.
     
  7. AdamBebko

    AdamBebko

    Joined:
    Apr 8, 2016
    Posts:
    161
    I would recommend using a standard serializer, or even the builtin unity json utility. Don't worry about performance until you run into actual problems. As long as you write your code to make it easy to change later, it's likely better to work on other parts of your game first.