Serialized Dictionary

It has been used frequently in all the jobs I have worked so far. It is inevitable for unity to have this and it will attract many users to unity. Unity should have a serializable dictionary that can be edited through editor.

This has come up a lot and I’m not sure it’ll ever come about.

There is a serialisable dictionary somewhere within one of Unity’s packages, forget where though. No property drawer for it though.

Though I think a lot of people want to serialise dictionaries where it’s unnecessary. I see a lot of folks getting Odin Inspector just to serialise/edit a dictionary with 3-4 entries on a monobehaviour… where a list would be just as performant.

It’s called SerializedDictionary but I cannot remember where that is either. I think either in the SRP/URP or Input System. The issue with this and similar implementations on github is that on serialization, the dictionary is converted to two synchronized Lists and then serialized, and when deserialized the opposite happens. If the dictionary has (tens of) thousands of entries, or you use it on a great number of scripts, the performance of that can have a severe impact on save/load performance both in editor and runtime.

Instead I would (rather easily) serialize a native dictionary to binary with Unity’s Serialization package.

I’ve used it in such a way that rather than having two Lists, I actually serialize this dictionary to and from a byte array (and optionally GZIP it) using the serialization callback events so that Unity takes care of serializing the byte[ ] (essentially it just writes the series of bytes into the scene file as-is):

[SerializeField] private byte[ ] m_SerializedDictionary;

This came in very handy to save a huge world data in the most efficient way while also supporting Undo/Redo for every operation, since the byte[ ] is serialized so quickly I could afford doing that on every user change, and an undo would just give me the previous byte array and regardless of the change made, I could just reload the map (or chunk) from that data and get the previous state. Real neat, reliable, fast, easy!

Think of a 3D voxel grid, if you’d save that info the usual way with game objects you’d have maybe 128 bytes or more per grid entry, not to mention the size of the scene file itself whose text format is rather verbose. Whereas the way I saved it, I had 8 or 16 bytes per grid entry (an index + flags) and zipping it could compress the remaining data to about 50% or less.

its in the UnityEngine.Rendering but its not working properly. sometimes giving errors.
Yes some people do that. not me. I want to be able to look up a specific data and get that data superfast.

That’s why i want unity to make a serializable dictionary. Because that way, they can make fastest serialized dictionary alive.

Hmm this is actually very interesting i might give it a try.