Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Feature Request Serialized Dictionary

Discussion in 'Editor & General Support' started by woir, Sep 6, 2023.

  1. woir

    woir

    Joined:
    Jul 17, 2021
    Posts:
    3
    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.
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    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.
     
    CodeRonnie and Kurt-Dekker like this.
  3. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,899
    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.
     
    Ne0mega likes this.
  4. woir

    woir

    Joined:
    Jul 17, 2021
    Posts:
    3
    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.
     
  5. woir

    woir

    Joined:
    Jul 17, 2021
    Posts:
    3
    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.