How to serialize a Dictionary?

I’m trying to modify the TagFrenzy plugin to use a dictionary instead of a list for the tags. The issue I’m running into is that the Dictionary is not being found when I try and use mySerializedDictionary = tagger.FindProperty("MyDictionary"); If I replace “MyDictioanry” with a list of the same name it is found.

Is this possible in unity?

Serialization of Dictonaries isn’t directly possible in Unity… BUT…

You can use the ISerializationCallbackReceiver. If you additionaly add CallbackReceiver to derive from (Instead of only MonoBehaviour, or something else), you can make changes before and after serialization. Like this you can run through your Dictonary and split it into two List in OnBeforeSerialization(). Lists can nativly be serialized with Unity. OnAfterDeserialize() you would iterate through the Lists and recreate your Dictonary.

Link to the Full explanation:


Like this, your Dictonary should no longer become empty everytime an Assembly gets reloaded, etc.

Regards Michael