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 !
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 ?
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
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.
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.