Search Unity

[Released] Dictionaries are now supported !!!! :D

Discussion in 'Scripting' started by astrokoala, Apr 24, 2016.

?

Would you buy it?

  1. At all cost!

    1 vote(s)
    2.2%
  2. Shut up and take my $10

    3 vote(s)
    6.5%
  3. No more than $5

    7 vote(s)
    15.2%
  4. I Demand you release it for FREE NOW

    35 vote(s)
    76.1%
  1. astrokoala

    astrokoala

    Joined:
    Mar 23, 2016
    Posts:
    22
    :DJust kidding, I went through a lot of hard work and research to get to this stage, I am still testing it, nailing out bugs, and will upload to asset store soon. I'm going to solve Dictionary for Unity once and for all!

    The Unity Dictionary Paradox
    (Read this section if you are unfamiliar with Dictionary<K,V> )
    Its now Version 5.4 and Unity still refuses to implement serializable and inspectable Dictionaries.
    But us programmers, we loves Dictionaries to put better organize our logic together.

    Some of us gave up using Dictionary, and try some gimmicky workaround, such as double array:(

    Some turned to solution like SQLite... But this is solving problem by introducing another problemo_O

    I Know some<T> solution

    Me too, I tried all the Dictionary solutions in Asset Store, couples of inspector enhancement plugins, but none of them is convincing enough to make me use Dictionary in unity.

    The common problems are:
    1. Ugly
    2. not real Dictionary (simulated with 2 lists, but List can contain duplicates, Dictionary can't)
    3. Doesn't behave like Dictionary in code (such as you must access its member like dict.Value)
    4. Pain in the ars to setup (None of them shows up in the inspector without writing a custom PropertyDrawer for each derived Dictionary class in a new file in a different directory:confused:)
    5. Limited (such as you cant use GameObject as key)
    6. VERY Ugly or not working when using complex types or nesting
    So you solved all above problems?

    Yes I solved 100% of the problem 97% of times, lets look at how I tackles those problems

    1. NOT UGLY

    I'm using the fancy new ReorderableList in UnityEditorInternal, it doesn't have Dictionary support, and its undocumented, so I've done a lot of hacking to get this in the inspector, not bad ey?:D

    Because all the input boxes for int, float, string etc are the same, I added the Type hint next to the variable name, It can't detect the exact name of a type, but still does the job of letting you distinguish between similar looking inputs

    2.+3. almost Real Dict
    okay, its not derived from Dictionary, but I've mimicked almost all interfaces, It still uses an actual System.Collections.Generic.Dictionary object at its core, so functionality and performance wise are the same

    check out screenshot below


    4. Never write Dictionary Editor || Inspector || PropertyDrawer again
    Not "Simple" setup, not 2 line code, Just declare your Dictionary and go, Lets see it in Action
    Code (CSharp):
    1. [Serializable]
    2. public class MyCustomDict : Dict<GameObject, float>{};
    MAGIC!:cool:
    All other solutions requires you to
    1. create your own class derived from a base class
    2. go to Editor Folder
    3. create another file
    4. create another class, derived from a base editor class
    No more Jumping back and forth when managing your own Dictionaries

    5. Create Any key Value Dictionary
    Remember, Dictionaries cannot have duplicate or null keys, so what happens if you get duplicated keys or null keys?
    no problems, my solution handles it for you.
    • Duplicated or invalid key will be highlighted in red
    • Entries with duplicated keys will not be available in runtime
    • Entries with duplicated keys will still be saved and serialized in editor, so you don't lose any data
    • In runtime, you cannot get duplicated keys just like native Dictionary
    Heck, create a <bool, bool> Dictionary if you desire


    6. Complex Types and nesting
    Believe me when I said I tried all other solutions, and none of them work well in this field, I have to Deduct 3% from my 100% score because this is still not perfect
    Check it out
    Code (CSharp):
    1.  
    2. [SerializeField]
    3. ComplexDict IHopeILookOk;
    4.  
    5. [Serializable]
    6. public struct ComplexStruct{
    7.     public int a;
    8.     public float b;
    9.     public Quaternion VeryLongNameIsAGoodPractice;
    10. }
    11. [Serializable]
    12. public class ComplexDict : Dict<string, ComplexStruct>{}
    13.  

    Its ugly by my standard, but thats Mainly I did not write a propertyDrawer for that ComplexStruct.
    BUT, it is still usable, the slider on the top controls the Key Value Divider, so you can have Big value small key, or big key small value. The point is even if your inspector tab is very narrow, you could still see your elements by adjusting the divider


    As you see, the divider is a really powerful feature, that let the Dictionary take on any kind of complex type without having to worry about not displaying properly


    Check out nesting in action

    Are you drooling for it now?
    I'll upload it very soon WITH Source code of course, so hold on to your saliva and PRAY Unity review it fast
     

    Attached Files:

    Last edited: Oct 10, 2017
  2. astrokoala

    astrokoala

    Joined:
    Mar 23, 2016
    Posts:
    22
    Partially fixes nested Dictionary

    now that child Dictionary shows correct result


    whats still broken:
    1. when adding entry to child Dictionary, it still adds to all other child dictionary
    2. when one of the child dictionary is folded, other child dictionary shows wrong result
     

    Attached Files:

  3. astrokoala

    astrokoala

    Joined:
    Mar 23, 2016
    Posts:
    22
    Check it out, All bugs nailed!
     

    Attached Files:

    Last edited: Apr 25, 2016
    hopeful likes this.
  4. astrokoala

    astrokoala

    Joined:
    Mar 23, 2016
    Posts:
    22
    Submitted to the asset store, stay tuned!
     
  5. starikcetin

    starikcetin

    Joined:
    Dec 7, 2017
    Posts:
    340
    Any updates?
     
  6. dCalle

    dCalle

    Joined:
    Dec 16, 2013
    Posts:
    55
    Damn, this looks nice mate ;-)
     
  7. Kasper_OmsK

    Kasper_OmsK

    Joined:
    Jul 9, 2015
    Posts:
    47
    huh... Is there an actual way to have an IDE in unity as it seems to be in your screenshots ?
    What kind of black magic is this ?
     
  8. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    Script Inspector maybe?
     
  9. Kasper_OmsK

    Kasper_OmsK

    Joined:
    Jul 9, 2015
    Posts:
    47
  10. astrokoala

    astrokoala

    Joined:
    Mar 23, 2016
    Posts:
    22
    wow a full year have passed, and i finally got a chance to submit it to the store, it is now live :)
    I'll give out 10 keys for people willing to write out honest reviews, please PM me about it
     
    hopeful likes this.
  11. Arany666

    Arany666

    Joined:
    Jun 25, 2015
    Posts:
    11
    HI ! I've bought the Dictator, tried it, crashes ...:(
    It should be working with custom classes, or not?
    in the editor it works fine, but when I try to look at the"Dict" runtime the whole Unity crashes
     
  12. Arany666

    Arany666

    Joined:
    Jun 25, 2015
    Posts:
    11
    I fill up the dict run time, with 300 - 500 key - value.
     
  13. Arany666

    Arany666

    Joined:
    Jun 25, 2015
    Posts:
    11
    using Unity 2017.1.1f1
     
  14. astrokoala

    astrokoala

    Joined:
    Mar 23, 2016
    Posts:
    22
    Arany 666 please check your inbox

    edit: fixed op images
     

    Attached Files:

    Last edited: Oct 10, 2017
  15. dousi96

    dousi96

    Joined:
    Jul 28, 2014
    Posts:
    23
    Hi,

    I've just bought this plugin because i have a lot of problems in trying to implements Dictionary compatible with Unity Inspector.
    I've tried this int my project but it doesn't seem to be atapt to my problem.
    I cant inspect the second nested dictionary and sometimes it gives me some strange errors in the console.
    (Another small bug: the foldoutArrow is inside the key field)

    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : TankStateController //it's monobehaviour
    6. {
    7.     public CiaoClips ciao;
    8.     public CiaoClipsDict ciaoDict;
    9. }
    10.  
    11. [Serializable]
    12. public class Clips
    13. {
    14.     [SerializeField]
    15.     public List<AudioClip> collection;
    16.  
    17.     public Clips() : base()
    18.     {
    19.         collection = new List<AudioClip>();
    20.     }
    21. }
    22.  
    23. [Serializable]
    24. public class LocClips : Dict<SystemLanguage, Clips> { }
    25.  
    26. [Serializable]
    27. public class CiaoClips
    28. {
    29.     [SerializeField, Range(0, 100)] private int priority;
    30.     [SerializeField] private LocClips collection;
    31. }
    32.  
    33. [Serializable]
    34. public class CiaoClipsDict : Dict<string, CiaoClips> { }
    If someone can help me please reply!

    Last thing, I think that implementing a constructor for the Dict class would be nice in order to have more control on the usage of the dict class.

    Thank you
     

    Attached Files:

    Last edited: May 4, 2018
  16. Pomettini

    Pomettini

    Joined:
    Aug 29, 2013
    Posts:
    26
    Hey Astrokoala, your plugin is awesome and I love it, but has this small graphical issue that drives me crazy, do you know how I can fix that? Thank you :)

     
  17. joeconstable97

    joeconstable97

    Joined:
    Jul 30, 2013
    Posts:
    50
    Hi mate,

    I know this is two years late so you've probably sorted this out by now but just in case, I found a fix for this!

    In the dictator editor script, at line 317, the Foldout is rendered...
    Added a line above that (but still in the if statement) with:
    Code (CSharp):
    1. pos.height = EditorGUIUtility.singleLineHeight;