Just 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 problem:eyes:
I Know some 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:
- Ugly
- not real Dictionary (simulated with 2 lists, but List can contain duplicates, Dictionary can’t)
- Doesn’t behave like Dictionary in code (such as you must access its member like dict.Value)
- 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:face_with_spiral_eyes:)
- Limited (such as you cant use GameObject as key)
- 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?
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
[Serializable]
public class MyCustomDict : Dict<GameObject, float>{};
MAGIC!
All other solutions requires you to
- create your own class derived from a base class
- go to Editor Folder
- create another file
- 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 here
[SerializeField]
ComplexDict IHopeILookOk;
[Serializable]
public struct ComplexStruct{
public int a;
public float b;
public Quaternion VeryLongNameIsAGoodPractice;
}
[Serializable]
public class ComplexDict : Dict<string, ComplexStruct>{}
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