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 programming environments https://docs.unity3d.com/Manual/script-Serialization.html If you want to have a custom serialization system, you are free to do so.

1 Answer

1

Dictionaries can be serialize, but unity does not do it.

There is even example in docs how to do it: Unity - Scripting API: ISerializationCallbackReceiver

There are some assets (free or not) to also draw it in inspector, one of them you can see there:

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.