- Home /
Serialize a Dictionary
According to the docs a Dictionary can't be serialized in Unity so I was thinking about buying an asset that can do that so that i can save my dictionaries to file, but I'm able to serialize and deserialize a Dictionary using the BinaryFormatter, I'm not sure I understand, can someone explain the difference between serialization and what the BinaryFormatter does?
I don't know where you have seen that Dictionary can't be serialized
. They are serializable, Unity simply does not do it.
Serializers in Unity run in a real-time game environment. This has a significant impact on performance. As such, serialization in Unity behaves differently to serialization in other program$$anonymous$$g environments
https://docs.unity3d.com/$$anonymous$$anual/script-Serialization.html
If you want to have a custom serialization system, you are free to do so.
That statement from Unity though, pure , they could just admit their serializer is badly written and can't be expanded at this point, dictionaries and graphs needed pretty much on all games.
As for the original question, don't mix up the Serializers, Unity's Serializer != c# serializer, what is Serializable via one may not be serializable via the other, they both make use of the [Serializable] attribute but that's all they have in common.
I did mix up the serializers, I don't really code outside of Unity so I tend to associate VS and coding with Unity, when I heard that Unity couldn't serialize Dictionaries I didn't understand why i was able to do so using the BinaryFormatter but now I get it.
Answer by Prastiwar · Oct 07, 2018 at 06:36 AM
Dictionaries can be serialize, but unity does not do it.
There is even example in docs how to do it: https://docs.unity3d.com/ScriptReference/ISerializationCallbackReceiver.html
There are some assets (free or not) to also draw it in inspector, one of them you can see there:
https://github.com/Prastiwar/UnitySerializedDictionary
Can you answer this, why am I able to save (serialize?) Dictionaries using the BinaryFormatter, isn't this serialization? This is what I'm trying to understand, I don't need to serialize Dictionaries to see them in the inspector, my goal is to save them to file using the BinaryFormatter and it seems to work, I'm trying to understand why and if this is something I should do or not before I start doing it a lot.
Yes, process of saving and loadind is simply called serialization
.
There is nothing wrong to save data with BinaryFormatter (also json, xml, prefs and others), they exists to do exactly that.
Why you can do that? Because Dictionary is serializable - that means you can save and load it.
You need to remember a key to serialize(save/load) data is [Serializable]
attribute on class/struct that allows it to be serialized, in unity also [SerializeField]
on fields, primitives are serializable by default.
Some rules to use serialization in Unity you can find THERE.
Also docs from $$anonymous$$SDN is also worth to read.
"There is nothing wrong to save data with BinaryFormatter ..."
What about object references? I'm not really sure how unity serializes these, but if the project contents change sufficiently, couldn't references stored via BinaryFormatter, end up pointing at the wrong object?