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

Serializable Dictionary Pro

Discussion in 'Assets and Asset Store' started by Rotary-Heart, Apr 16, 2020.

  1. wozniaxen

    wozniaxen

    Joined:
    Oct 31, 2018
    Posts:
    16
    Hello I noticed some weird behaviors in inspector with Pro version. Sending a video showing it.
    I also noticed that when I change Key from for example "HIGH" to "VERY HIGH" all sliders are reset to 0.
     

    Attached Files:

    Last edited: Apr 2, 2022
  2. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    804
    Seems like the nested dictionary bug is back. I'll check that out when I'm back too. I had a report for that before.
     
  3. Devizen

    Devizen

    Joined:
    Sep 12, 2017
    Posts:
    2
    Hello Rotary-Heart, I made a purchased for the Pro version after using your Lite version for some time. I have tried the SerializableSortedDictionary class and it is working correctly for data. However, when I add a new Key to the class on the inspector, the data displayed in the inspector is not sorted. Is this the intended behaviour? I would like it to be sorted on the inspector whenever a new Key is added via the inspector. Thanks!
     
  4. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    804
    Hello, yes that is the current behavior since the editor doesn't use the dictionary at all. However, I could add the functionality of being sorted automatically.

    When I'm back from vacation, I'll work with this after fixing the bugs posted above.
     
    Devizen likes this.
  5. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    804
    Version 3.3.5 has been submitted to the asset store for review. This version includes:
    • Fixed bug with nested dictionaries
    • Modified sorted dictionary to have the editor side using the same order as the dictionary
    • Fixed bug with adding data directly to the dictionary wasn't adding it to the editor side
     
    wozniaxen likes this.
  6. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    804
    Great News! Version 3.3.5 is now live on the store!
     
  7. wozniaxen

    wozniaxen

    Joined:
    Oct 31, 2018
    Posts:
    16
    Thank you for bug fixes :)

    I found another small bug but a little annoying.
    When I change a key all values are reset to 0, even on other keys.

    Added video as an attachment.
     

    Attached Files:

    • bug.gif
      bug.gif
      File size:
      251 KB
      Views:
      176
  8. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    804
    I've identified and found a possible fix for the issue. Going to send you the file for testing before pushing the update to the asset store
     
    Stexe likes this.
  9. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    804
    Version 3.3.6 has been been submitted to the asset store for review. This version includes:
    • Performance fixes
    • Fixed error where trying to add an element to a enum key dictionary when all keys were used would throw an exception
    • Fixed error with enum key dictionary resetting values
     
  10. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    804
    Great News! Version 3.3.6 is now live on the store!
     
  11. ifightnoman

    ifightnoman

    Joined:
    Jan 15, 2022
    Posts:
    13
    With the Pro version, I have a simple setup with a SerializedDictionary that should have a list of sprites as its values:

    Code (CSharp):
    1.  
    2. [Serializable]
    3. public class SpriteComponentDictionary : SerializableDictionary<string, Sprite[]> { }
    4.  
    5. [CreateAssetMenu(fileName = "CharacterAsset", menuName = "Character Asset")]
    6. public class CharacterAsset : ScriptableObject {
    7.     public CharacterType Type;
    8.     public SpriteType SpriteType;
    9.     public SpriteComponentDictionary Components;
    10.     public GameObject Prefab;
    11. }
    But there's no way to edit the value collection in the Inspector: I can only add keys. Since I need to drag hundreds of sprites from various locations into the scriptable object if I can't edit it in the Inspector this is basically useless.

    upload_2022-5-6_15-39-16.png
     
  12. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    804
    Hello, thank you for your purchase.

    That's an unfortunate limitation on Unity serialization. You need to wrap your value type in a class as following:
    Code (CSharp):
    1. [Serializable]
    2. public class Data
    3. {
    4.     public Sprite[] values;
    5. }
    6.  
    7. [Serializable]
    8. public class SpriteComponentDictionary : SerializableDictionary<string, Data> { }
    9.  
    10. [CreateAssetMenu(fileName = "CharacterAsset", menuName = "Character Asset")]
    11. public class CharacterAsset : ScriptableObject {
    12.     public CharacterType Type;
    13.     public SpriteType SpriteType;
    14.     public SpriteComponentDictionary Components;
    15.     public GameObject Prefab;
    16. }
    You can read more about it in the Advanced Dictionary section of the documentation
     
  13. ifightnoman

    ifightnoman

    Joined:
    Jan 15, 2022
    Posts:
    13
    Oh wow, little clunky but it works now! Thanks for the quick response. That info should probably go right on the front of the asset store readme, since it's a bit unintuitive.
     
  14. Devizen

    Devizen

    Joined:
    Sep 12, 2017
    Posts:
    2
    Hey Rotary-Heart, thanks for the update. I have another question which I couldn't solve.
    I am currently making a custom editor and is trying to access the SerializedDictionary via SerializedProperty.FindPropertyRelative("Key").
    The SerializedProperty that I am calling the FindPropertyRelative function is the SerializedDictionary.
    However, I am not retrieving the value from the SerializedDictionary.
    May I know what is the right step to retrieve the value when using SerializedProperty?

    Code (CSharp):
    1.  
    2. [System.Serializable]
    3. public class ExampleDictionary: RotaryHeart.Lib.SerializableDictionaryPro.SerializableDictionary<SomeENUM, ExampleClass> { }
    4.  
    5. [System.Serializable]
    6. public class AnotherClass : ScriptableObject
    7. {
    8.     [SerializableField]
    9.     private ExampleDictionary m_ExampleDictionary = new ExampleDictionary();
    10. }
    11.  
    12. public class ExampleWindow : EditorWindow
    13. {
    14.     private AnotherClass m_AnotherClass;
    15.  
    16.     private void OnEnable()
    17.     {
    18.         m_AnotherClass = ScriptableObject.CreateInstance<AnotherClass>();
    19.         SerializedObject anotherClassSerializedObject = new SerializedObject(m_AnotherClass);
    20.         SerializedProperty exampleDictionarySerializedProperty = anotherClassSerializedObject.FindProperty("m_ExampleDictionary"); //Can be found.
    21.         SerializedProperty exampleClassSerializedProperty = exampleDictionarySerializedProperty.FindPropertyRelative(SomeENUM.value.ToString()); //null
    22.     }
    23. }
    Above are some example codes that I typed out for you to understand my issue. Might have some errors because I have not tested the code.
    Thank you.

    Updated:
    It seems like I have to get the "m_keys" and "m_values" from the SerializedDictionary to do manual comparison for the key before extracting the value. Is there an easier way for me to access the value?

    Updated 2:
    Since my ENUM starts from 0 and increment by 1 without interval, I could just access m_values at index (int)ENUM.value and make it work. Will need a for loop if I ever use ENUM with differing values.

    Updated 3:
    I have decided to use a List directly instead of SerializedDictionary for this matter as I can access the index directly through (int)ENUM.value.

    Just updating it here in case anyone is interested.
     
    Last edited: May 8, 2022
  15. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    804
    Hello,

    Yes, the proper names are m_keys and m_values. What are you trying to do with your custom editor? The dictionary has a couple of editor only callbacks that you could use (depending on what you intend to do with your editor)

    • EditorOnRemove
    • EditorOnAdded
    • EditorOnDrawElement
    • EditorOnReorder
    • EditorOnEdit
    You can see them at DrawbleDictionary class
     
  16. zephybite0

    zephybite0

    Joined:
    Apr 25, 2018
    Posts:
    29
    Hi, I've been using this for two years and it's been great. I recently discovered that an exception is thrown when you try to reassign a key that's "Missing" - see screenshots. Wondering if you know of a fix for this?
     

    Attached Files:

    jGate99 likes this.
  17. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    804
    Hello,

    I'm glad to see that the asset has been of good usage for your projects.

    Thanks for this report! While trying to replicate your error I found a bug on current version, I'm uploading a quick update to the asset store now, but I couldn't replicate your error. Perhaps the bug fix fixes your issue too. I'll post here the patch changes so you can apply them and let me know. I can give your more details in PM if you want.

    Note, you might need to change the file path to .diff (depends on what you use to apply it)
     

    Attached Files:

  18. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    804
    Version 3.3.7 has been submitted to the asset store for review. This version includes:
    • Fixed error while trying to add elements setting key to default
     
  19. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    804
    Great News! Version 3.3.7 is now live on the store!
     
    zephybite0 likes this.
  20. zephybite0

    zephybite0

    Joined:
    Apr 25, 2018
    Posts:
    29
    I think something's wrong with the update ^^; When I tried to download the update I got stuck in a "Reload Script Assemblies" dialogue. I tried deleting Library + Temp folders but I still get stuck on importing the dictionary scripts. I'm using 2021.3.5f1 LTS. I ended up just reverting to get the old version back.
     

    Attached Files:

  21. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    804
    Do you by any chance have both the lite and pro versions installed in the project? I can't seem to replicate this issue
     
  22. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    804
    Other member helped me replicate the issue, I just submitted a quick patch that prevents this issue @zephybite0
     
    zephybite0 likes this.
  23. zephybite0

    zephybite0

    Joined:
    Apr 25, 2018
    Posts:
    29
    Thanks! That seems to have worked :)
     
    Rotary-Heart likes this.
  24. Timmy-Hsu

    Timmy-Hsu

    Joined:
    Aug 27, 2015
    Posts:
    50
    Hi,
    Is it possible to work in Unity 2017.4.40?
     
  25. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    804
    Hello, yes it should work with any Unity version, unfortunately we are not allowed to upload with older unity versions.
     
  26. Timmy-Hsu

    Timmy-Hsu

    Joined:
    Aug 27, 2015
    Posts:
    50
    I use Unity 2017.4.40f1 to create new project, and import Serialized Dictionary PRO_3.3.7,
    then will got this error seems about asmdef.

    Assembly has reference to non-existent assembly 'GUID:10ebc479cc081044d87faf0f7bd87988' (Assets/Rotary Heart/SerializableDictionaryPro/Editor/RotaryHeart.SerializableDictionaryPro.Editor.asmdef)
     
  27. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    804
    How did you import the package? Directly from the asset store window? Seems like you are missing the Assets/Rotary Heart/Shared/ folder.
     
  28. Timmy-Hsu

    Timmy-Hsu

    Joined:
    Aug 27, 2015
    Posts:
    50
    Assets -> Import Package -> Custom Package
    Snipaste_2022-07-22_17-44-17.jpg

    Shared Folder
    Snipaste_2022-07-22_17-44-24.jpg
     
  29. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    804
    Hmmm it might be that 2017 doesn't like using GUID in the asmdef, here replace that asmdef with this one. You'll have to rename the file and remove the .txt extension. The forums wouldn't let me upload the .asmdef directly
     

    Attached Files:

    Timmy-Hsu likes this.
  30. Timmy-Hsu

    Timmy-Hsu

    Joined:
    Aug 27, 2015
    Posts:
    50
    Thank you!
    Now It works on Unity 2017.
     
    Rotary-Heart likes this.
  31. Raseru

    Raseru

    Joined:
    Oct 4, 2013
    Posts:
    87
    Minor bug and I don't need an individual patch for it just a FYI:
    Creating a dictionary with an enum as a key that doesn't have a value assigned to 0 (like let's say it only has one enum and its set to value 30) will bug out and can't be assigned as the inspector is cut off at 'Element to add' as can be seen below.


     
    Rotary-Heart likes this.
  32. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    804
    Nice catch! I could replicate the issue and found a fix for it. I'll upload an update to the store.
     
    Raseru likes this.
  33. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    804
    Version 3.3.8 has been submitted to the asset store for review. This version includes:
    • Fixed bug that happens when using an enum as key that doesn't have a value of 0
     
  34. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    804
    Great News! Version 3.3.8 is now live on the store!
     
  35. amenonegames

    amenonegames

    Joined:
    Nov 10, 2021
    Posts:
    16
    Hi! I'm using Serialized Dictionary PRO in ScriptableObject.
    But When I use List<SomeMonoBehaviour> as TValue, Value will be vanish when restrat Unity or recompile.
    Is there some solusions ?

    When I reregister the same keyValue that onece vanished, Console show following error message,
    "ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection."

    code is following ..

    ```
    [System.Serializable]
    public class PrefabDictionary :SerializableDictionary<string, List<GameObject>>
    {
    }
    ```
    ```
    [CreateAssetMenu(fileName = nameof(RoomPrefabs), menuName = "Callme/" + nameof(RoomPrefabs))]
    public class RoomPrefabs : ScriptableObject
    {
    public PrefabDictionary _prefabs;
    }
    ```

    I registered like this.
    upload_2022-11-21_20-27-16.png


    But Once Unity Resterted. like this.
    upload_2022-11-21_20-30-15.png
     
    Stexe likes this.
  36. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    804
    Hello, that's an unfortunate limitation of Unity serialization system. You can work around that problem by wrapping your value into a struct/class, for more information check the Advanced Dictionary section in the documentation
     
    amenonegames and Stexe like this.
  37. amenonegames

    amenonegames

    Joined:
    Nov 10, 2021
    Posts:
    16
    Thank you! I understand.
     
    Rotary-Heart likes this.
  38. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    804
    Version 3.3.9 has been submitted to the asset store for review. This version includes:
    • Tooltip support is now implemented
     
  39. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    804
    Great News! Version 3.3.9 is now live on the store!