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. Join us on March 30, 2023, between 5 am & 1 pm EST, in the Performance Profiling Dev Blitz Day 2023 - Q&A forum and Discord where you can connect with our teams behind the Memory and CPU Profilers.
    Dismiss Notice

[Released] Serializable Dictionary Lite - Now allowing custom editor for key field

Discussion in 'Assets and Asset Store' started by Rotary-Heart, Feb 19, 2018.

  1. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    798
    Serializable Dictionary Lite
    CoverImage_1950x1300.png
    Website: RotaryHeart.com
    Product Wiki: Online

    Version 2.6.9.6

    Available Now: Asset Store

    Release Notes

    Pro version available

    UPM Version available

    Serializable Dictionary Lite is a system that I built for my projects since I needed to have a database like structure that could be modified on the editor by any designer. It can easily be used for any kind of serializable type either for key or for value. It contains a script that handles the inspector draw in an elegant and simple UI. The best part of the system is that it's easy to setup and it doesn't require any new scripts. All you have to do is declare your dictionary like:

    Code (CSharp):
    1. [Serializable]
    2. public class MyDctionary : SerializableDictionaryBase<Key, Value> { }
    Where key and value can be either Unity components or a simple type.


    Code (CSharp):
    1. [Serializable]
    2. public class MyDictionary : SerializableDictionaryBase<Material, GameObject> { }
    3. [Serializable]
    4. public class MyDictionary : SerializableDictionaryBase<string, float> { }
    5. [Serializable]
    6. public class MyDictionary : SerializableDictionaryBase<string, MyCustomClass> { }
    7.  
    Screenshot_2048x1152_Compatible with Light and Dark theme.png
    Screenshot_2048x1152_User friendly UI, with reordering.png
    Screenshot_2048x1152_Any serializable type as key.png
    Screenshot_2048x1152_ Easy to use, unique, and optional new page system.png
     
    Last edited: Dec 10, 2022
    Stexe, wulcat, Kerozard and 3 others like this.
  2. genaray

    genaray

    Joined:
    Feb 8, 2017
    Posts:
    189

    Hey there thats looking great ! First of all thanks for replying on my thread ! :) If you dont mind, i would love to take a look at your package ! Its looking pretty good in the pictures ! ^^
     
  3. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    798
    Great news! Version 1.0 is live on the asset store!
     
  4. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    798
    Product wiki has been published. It can be found at my page Wiki section
     
  5. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    798
    Version 1.0.1 has been submitted to the asset store for review. This version includes:
    • Removed the checks for UNITY_EDITOR on the dictionary class. This made the built shared assets file to be corrupt.
     
  6. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    798
    Version 1.0.1 is now live
     
  7. IVPathfinder

    IVPathfinder

    Joined:
    Feb 13, 2017
    Posts:
    4
    Unfortunately this is not what I wanted to use (not your fault, what you've written is awesome)

    I wanted to serialize data into json format for google firebase in a nested format like this:
    (to allow maximum nosql performance)

    Code (CSharp):
    1.  
    2. {
    3.         key1: {
    4.                 int1: 0,
    5.                 int2: 12
    6.         },....
    7.         key7: {
    8.                 int1: 0,
    9.                 int2: 12
    10.         }
    11. }
    12.  
    From what I see in plugin will not produce the wanted format
    Code (CSharp):
    1.  
    2.         [UnityEngine.SerializeField] private TKey[] _keyValues;
    3.  
    4.         [UnityEngine.SerializeField] private TKey[] _keys;
    5.  
    6.         [UnityEngine.SerializeField] private TValue[] _values;
    7.  
    I'll try to create a custom UnityEngine.ISerializationCallbackReceiver.OnBeforeSerialize()

    Main challenge is to get the key names to appear in the root position of the json... not sure if this is possible

    Probably have to use this:
    https://stackoverflow.com/questions...o-dictionaryint-string-from-custom-xml-not-us

    Or jsonbuilder:
    https://stackoverflow.com/questions/8876089/how-to-fluently-build-json-in-java
     
    Last edited: Apr 2, 2018
  8. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    798
    @IVPathfinder I'm sorry that this doesn't provide the output you expect. Perhaps we could work it out, but I fail to understand what you are trying to do. Can you provide a more in depth example?
     
  9. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    798
    Version 1.1 has been submitted to the asset store for review. This version includes:
    • Fixed an issue with int as key values.
    • Included new Quaternion better layout.
     
    rakkarage likes this.
  10. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    798
    Version 1.1 is now live on the asset store.
     
  11. andm-dlw

    andm-dlw

    Joined:
    Feb 7, 2017
    Posts:
    10
    Hi, I seem to have issues when my Value Type is an array of Unity Objects, e.g. SerializableDictionaryBase<string, GameObject[]>. In this case, the Property Drawer crashes as ValuesProp seems to be null.

    This is the Stack Trace:
    NullReferenceException: Object reference not set to an instance of an object
    RotaryHeart.Lib.SerializableDictionary.DictionaryPropertyDrawer.GetPropertyHeight (UnityEditor.SerializedProperty property, UnityEngine.GUIContent label) (at Assets/SerializableDictionary/SerializableDictionary/Editor/DictionaryPropertyDrawer.cs:71)

    My code is as follows:

    [SerializeField]
    private ObjectsDictionary _objects;

    [System.Serializable]
    public class ObjectsDictionary : SerializableDictionaryBase<string, GameObject[]> { }



    EDIT:
    Never mind, it seems that you can solve this by using a class with the array as a field, like so:

    [SerializeField]
    private ObjectsDictionary _objects;

    [System.Serializable]
    public class ObjectsDictionary : SerializableDictionaryBase<string, MyObjects> { }

    [System.Serializable]
    public class MyObjects
    {
    public GameObject[] objects;
    }
     
    Last edited: Apr 11, 2018
  12. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    798
    Hello,

    Unfortunately I haven't found a way to allow arrays as values. Unity doesn't recognize them as a SerializedProperty, that's why it throws that error, so the workaround is to wrap them in a class the way you already did.
     
    Ultroman likes this.
  13. Lecht

    Lecht

    Joined:
    Jul 1, 2014
    Posts:
    24
    This seems to not work when used within a ScriptableObject. After saving the project and reloading, the values are lost.
     
  14. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    798
    What do you mean? The example provided is with a ScriptableObject and it works fine.
     
  15. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    798
    Version 1.2 has been submitted to the asset store for review. This version includes:
    • Fixed a bug where values that contains other serializable dictionary were being synchronized.
     
  16. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    798
    Version 1.2 is now live on the asset store!!
     
  17. TarasHalynskyi

    TarasHalynskyi

    Joined:
    Mar 19, 2018
    Posts:
    2
    Hello, thank you for update. But unfortunately after update to 1.2 I have same issues with synchronized dictionaries and Enums. I uploaded the file to reproduce the problems.

    Here is screenshot:
    https://prnt.sc/j65dp1
     

    Attached Files:

  18. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    798
    That's odd, can you remove the asset and import it back. Make sure to download the update. I used your script for figuring out the issue and I don't have that synchronized error anymore. As for the Enum I am still trying to figure out how to allow a custom default value.

    Capture.PNG
     
    TarasHalynskyi likes this.
  19. TarasHalynskyi

    TarasHalynskyi

    Joined:
    Mar 19, 2018
    Posts:
    2
    Thank you. Really I reimported asset but didn't updated in download manager. Noob... =)
    Issue with synchronized dictionaries is fixed. Nice, thank you vary much. Waiting for further updates.
    And one little advice, update version of asset in documentation file for better understanding.
     
  20. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    798
    Good to know that is working now. That is the documentation version, not the asset version I will modify it to include both versions.
     
  21. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    798
    Version 1.3 has been submitted to the asset store for review. This version includes:
    • Added check to avoid getting a null reference if the Dictionary is modified on Awake.
    • Char and Gradient can now be used as keys.
     
  22. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    798
    Version 1.3 is now live on the asset store!!
     
  23. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    798
    New reordable list has been implemented into the dictionary UI. Currently on closed beta testing, but it seems like everything is going smooth. Here's a quick preview of how it looks. This allow for easier ordering, adding and removal. It even allows multiple remove and reordering.

    Capture.PNG Capture1.PNG
     
    rakkarage and hopeful like this.
  24. Rotock

    Rotock

    Joined:
    Jun 29, 2014
    Posts:
    18
    Dictonary count goes 0 and get this error: "ObjectDisposedException: The object was used after being disposed." after unity recompiles something. The serialization seems not to work.

    Using: Unity 5.6.5f1

    EDIT: Nevermind.
    Realy everyone invovled has to be marked: System.Serializable,SerializeField

    I have a Dictonary with transform key's and a custom class that saves a copy of all local values from the Transform.

    I had to mark the CustomClass as [System.Serializable] and the Vector, Quaternion fields as [SerializeField]

    Code (CSharp):
    1.  
    2.     [System.Serializable]
    3.     public class TransSavesDic : SerializableDictionaryBase<Transform, TransformExt> { }
    4.  
    5.     [SerializeField]
    6.     public TransSavesDic saveDic;
    7.  
    8.     [System.Serializable]
    9.     public class TransformExt
    10.     {
    11.         [SerializeField]
    12.         public Vector3 position;
    13.  
    14.         [SerializeField]
    15.         public Quaternion rotation;
    16.  
    17.         [SerializeField]
    18.         public Vector3 scale;
    19.         .........................
    20.      }
    21.  

    Only then, nothing gets thrown out in the emptiness, after Recompile or Play

    And i can say: it's dam creepy, then all the face bones get random values, because of Null, NaN in the vectors, rotations. :confused:
     
    Last edited: Apr 30, 2018
  25. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    798
    What types are you using for your dictionary? Are you using your dictionary on a ScriptableObject?
     
  26. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    798
    Yes, you need to mark any non monobeahviour class as Serializable for Unity to serialize it, that's just how Unity serialization works. Public fields don't need to be marked as SerializeField, public fields are serialized by default if the type is serializable.

    I don't understand what you mean with your last sentence. What you mean is creepy?

    Also if your key is the Transform, why do you want to save the values of it?
     
  27. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    798
    Version 2.0 has been submitted to the asset store for review. This version includes:
    • Reorderable list implemented.
    • Fixed error with gradient dictionary.
     
  28. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    798
    Version 2.0 is now live!
     
  29. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    798
    Version 2.0.1 has been submitted to the asset store for review. This version includes:
    • Updated documentation.
    • New folder structure.
     
    Mark_01 likes this.
  30. dousi96

    dousi96

    Joined:
    Jul 28, 2014
    Posts:
    23
    Will this plugin ever support generics Values types?
     
  31. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    798
    Hello,

    It has always support generic value type. Did you find something that doesn't work?
     
    Mark_01 likes this.
  32. dousi96

    dousi96

    Joined:
    Jul 28, 2014
    Posts:
    23
    (EDIT: The problem was that my scriptable object class was into the same .cs file as other classes)

    Yeah,
    I have some classes that extend SerializableDictionaryBase but in the inspector of the scriptable object i cannot see the custom inspector of the dictionary.
    (In a monobehaviour i can see the custom inspector)
    1.png 2.png
     
    Last edited: May 7, 2018
  33. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    798
    That's odd, could you send me the scripts you are using? You can PM them if you don't want to post them here.

    It should work fine on the ScriptableObject too the example provided is a ScriptableObejct.
     
  34. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    798
    Version 2.0.2 has been submitted to the asset store for review. This version includes:
    • Fixed bug where generic values that are a serializable dictionary are being drawn on top of the keys.
     
  35. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    798
    Version 2.0.2 is now live!
     
    dousi96 likes this.
  36. dousi96

    dousi96

    Joined:
    Jul 28, 2014
    Posts:
    23
    Hi guys,

    Congratulations, you done a really good job with the last update.
    I've a feedback for you, I'm working with Enums as TKey in the dictionary.
    Can you create a EnumPopup that shows only the selected one and the not used in the dictionary in order to avoid the user to select an already existing key.
    Thank you for your work!
     
  37. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    798
    Hello,

    Thanks for your kind words! I really appreciate knowing that there are people enjoying my assets.

    As for the feature, I think that is a pretty good idea. I will start working on finding a way to handle that. If you or anyone else have any other suggestion feel free to let me know.
     
  38. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    798
    I was able to do the enum request from @dousi96, is under testing right now, but everything seems to be working perfectly. It will be published soon to the asset store.
     
  39. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    798
    Version 2.1 has been submitted to the asset store for review. This version includes:
    • New enum key drawer where only available options are available on the drop down.
    • Included new nested dictionary example
     
  40. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    798
    Version 2.1 is now live!
     
  41. DidzTM

    DidzTM

    Joined:
    Jan 30, 2018
    Posts:
    17
    Hi! Great job done here!

    But do have any idea how to implement a custom serialized class to your work?
    For the moment, it's throwing:
    Key Type not implemented: Generic
    UnityEngine.Debug:LogError(Object)

    Did I miss something?

    Rgds
     
  42. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    798
    Hello,

    No you are not missing something. I haven't finished implementing generic types as key. I am working on it, hopefully it will be finished for next update. Sorry for the troubles that this may cause.
     
  43. Timmy-Hsu

    Timmy-Hsu

    Joined:
    Aug 27, 2015
    Posts:
    47
    Hi,
    Can this add unity Dictionary data to this SerializableDictionaryBase?
    I want use SerializableDictionaryBase to contains unity Dictionary

    Like this code from SerializableDictionary plugin

    [Serializable]
    public class StringStringDictionary : SerializableDictionary<string, string> {}

    [SerializeField]
    StringStringDictionary m_stringStringDictionary;
    public IDictionary<string, string> StringStringDictionary
    {
    get { return m_stringStringDictionary; }
    set { m_stringStringDictionary.CopyFrom (value); }
    }

    StringStringDictionary = new Dictionary<string, string>() { {"first key", "value A"}, {"second key", "value B"}, {"third key", "value C"} };
     
  44. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    798
    Welcome to the forums, for future code please use the code tags when posting code. It makes it easier to read.

    While this asset doesn't have a Copy function you can certainly add that functionality without problems. Here's a quick example. Attach this to any GameObject and you will see that the values are added on start.

    Code (CSharp):
    1. [System.Serializable]
    2.     public class DictionaryCLass : SerializableDictionaryBase<string, string> { }
    3.  
    4.     [SerializeField]
    5.     DictionaryCLass m_stringStringDictionary;
    6.  
    7.     public IDictionary<string, string> StringStringDictionary
    8.     {
    9.         get { return m_stringStringDictionary; }
    10.         set
    11.         {
    12.             //Instead of using the CopyFrom function you can implement your own here
    13.             foreach (var t in value)
    14.             {
    15.                 m_stringStringDictionary.Add(t.Key, t.Value);
    16.             }
    17.         }
    18.     }
    19.  
    20.     void Start ()
    21.     {
    22.         StringStringDictionary = new Dictionary<string, string>() { { "first key", "value A" }, { "second key", "value B" }, { "third key", "value C" } };
    23.     }
    I will include a copy function for future updates since this is not the first time I got this request.
     
    Timmy-Hsu likes this.
  45. Timmy-Hsu

    Timmy-Hsu

    Joined:
    Aug 27, 2015
    Posts:
    47
    Hi,

    Thanks for reply.
    I try it works!

    I have another question.
    Can it use custom class as Key?

    Like this code:
    [System.Serializable]
    public class MyKey
    {
    public int iKey;
    public string strKey;
    }

    [System.Serializable]
    public class DictionaryCasesDict : SerializableDictionaryBase<MyKey, string> { }
    [SerializeField]
    DictionaryCasesDict _TestCasesDict;
    public IDictionary<MyKey, string> m_TestCasesDict
    {
    get { return _TestCasesDict; }
    set
    {
    //Instead of using the CopyFrom function you can implement your own here
    foreach (var t in value)
    {
    _TestCasesDict.Add(t.Key, t.Value);
    }
    }
    }


    I try it, but when I try to edit key value ,it has error log:
    Key Type not implemented: Generic
    UnityEngine.Debug:LogError(Object)
    RotaryHeart.Lib.SerializableDictionary.DictionaryPropertyDrawer:GetKeyValue(SerializedProperty) (at Assets/SerializableDictionary/Editor/DictionaryPropertyDrawer.cs:778)
    RotaryHeart.Lib.SerializableDictionary.DictionaryPropertyDrawer:List_drawElementCallback(Rect, SerializedProperty, GUIContent, Int32, Boolean, Boolean) (at Assets/SerializableDictionary/Editor/DictionaryPropertyDrawer.cs:578)
    RotaryHeart.Lib.SerializableDictionary.ReorderableList:DrawElement(SerializedProperty, Rect, Int32, Boolean, Boolean) (at Assets/SerializableDictionary/ReorderableList/ReorderableList.cs:587)
    RotaryHeart.Lib.SerializableDictionary.ReorderableList:DrawElements(Rect, Event) (at Assets/SerializableDictionary/ReorderableList/ReorderableList.cs:497)
    RotaryHeart.Lib.SerializableDictionary.ReorderableList:DoList(Rect, GUIContent) (at Assets/SerializableDictionary/ReorderableList/ReorderableList.cs:289)
    RotaryHeart.Lib.SerializableDictionary.DictionaryPropertyDrawer:OnGUI(Rect, SerializedProperty, GUIContent) (at Assets/SerializableDictionary/Editor/DictionaryPropertyDrawer.cs:274)
    UnityEngine.GUIUtility:processEvent(Int32, IntPtr)
     
  46. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    798
    Version 2.3 has been submitted to the asset store for review. This version includes:
    • Implemented user fix where iterating an empty dictionary could throw an error.
    • Implemented user fix for GetPropertyHeight, elements were getting stuck at the bottom of the drawer.
    • New copy function implemented on the dictionary.
    • Included new support window on the editor.
    • Fixed drawing color, now it should be easier to read on light theme editor.
    • Implemented generic type for key. Drawer will now correctly draw generic key types.
    • Modified GetPropertyHeight function to take into consideration the new generic type keys.
    • Added new generic type key examples.
    @unity_FYdm1oKZvIpiGA and @Timmy-Hsu this new update will let you use Generic type for key.
     
    Timmy-Hsu likes this.
  47. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    798
    Great news, version 2.3 is now live!
     
    Timmy-Hsu likes this.
  48. waxx

    waxx

    Joined:
    Apr 1, 2013
    Posts:
    48
    I wish that upon adding a new entry with the Enum key the plugin would look for the next available enum value.
     
  49. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    798
    This feature is coming with the next update, which should be ready to be published today.
     
  50. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    798
    Version 2.4 has been submitted to the asset store for review. This version includes:
    • Enum key now shows disabled values that are used instead of not drawing it.
    • Removed old unused code.
    • Modified the enum key dictionaries to try and get the next available key when a new entry is added instead of defaulting to the first enum element.