Simple Example of DictionaryDraw

I found on an old thread :

I just wanted a dictionary which can be editable on the unity inspector. Is there a way to do this easily or I need to download all of that ?

there is a dictionary in .Net - the answer really depends on what you need the dictionary for…

if its just for paired keys, there is no reason to use the above.

look at Dictionary<TKey,TValue> Class (System.Collections.Generic) | Microsoft Learn

I’ve not looked at the GitHub code (sorry - lazy me) - but the serializable lib might be good if you need to save the dictionary between sessions…

This is the Dictionary form .NET I did like to use, the problem is, Unity inspector doesn’t show dictionnaries.

Maybe, know it’s works, but I want with one of the first version of Unity5, and dictionaries are not visible in the inspector.

add
using System.Collections.Generic;
to the top of the file…

Does it works for you ?

When I look on Google, I found many persons who got the same problem then me.

I see; you want it to show there :wink:
Well, the problem is a little more difficult then :slight_smile: Dictionary is a dynamic length - meaning that you potentially would have 10000000s of entries in it…
Let me have some more info on what you want to use it for, and I’ll think about how to wip up something you can actually use - otherwise - download the above code (which you linked) if that will solve your problems…
Sorry I am not much more help …

Unity’s serializer only serializes certain types including primitive types, classes, structs and Generics only when the type is known IE you have to inheret the generic class. Dictionaries are not support by the Unity Serializer and this is why it doesn’t show up in the inspector. You have to write your own Serializer for Dictionaries if you want it to work correctly in the inspector and yes its a pain in the ass hence all those repos.

I make a class (Objet & ObjetEditor). I want to make an invetory script now. My inventory will consist of a list of Objet, and every Objet got only 1 quantity (objet key, int quantity value). I need to got information easily on the inspector because I work w/ someone who don’t see code.

I currently made a sort of dictionary (without function), which look very bad on the Inspector. I don’t want to waste too much time to do a proper Drawer / Editor. There is no free dictionary in the Asset Store and easy to use ?

@anon_21420655 : Like Polymorphic said - its a PITA, I’d like to help you, but really, those repos are your best friends for what you want.
@Polymorphik : Yeah, misread the request by OP, but I agree…

Yeah I think this is where Unity ScriptableObject would work best for you.

@anon_21420655 : If you’d like some help with it - message me - I’m free for a bit atm… but really can only spare today / maybe tomorrow…

I did this more for my own educational purposes… perhaps you might find it handy…(This post’s code is MIT license…i…)

                this.Name = Name;
                this.Next = null;
            }
            internal void SetQuantity(int Quantity) { this.Quantity = Quantity; }
            internal void SetName(string Name) { this.Name = Name; }
            internal void SetNext(ListData Next) { this.Next = Next; }
            internal void SetPrevious(ListData Previous) { this.Previous = Previous; }
        }



        private ListData _head = null;
        private ListData _tail = null;
        public List() { this._count = 0; }
        public int _count { get; private set; }
        public ListData GetFirst() { return this._head; }
        public ListData GetNext(ListData Node) { return Node.Next; }
        public int Add(string Name, int Quantity)
        {
            _count++;
            ListData Node = new ListData(Name, Quantity);
            if (this._head == null)
                this._head = this._tail = Node;
            else
            { this._tail.SetNext(Node); Node.SetPrevious(this._tail); this._tail = Node; }
            return _count;
        }
        public void Update(string Name, int Quantity)
        {
            ListData Node = this._head;
            while (Node != null)
                if (Node.Name != Name) Node = Node.Next;
                else { Node.SetQuantity(Quantity); break; }
        }
        public bool Contains(string Name)
        {
            ListData Node = this._head;
            while (Node != null)
                if (Node.Name == Name) return true;
                else Node = Node.Next;
            return false;
        }
        public int Remove(string Name)
        {
            if (!this.Contains(Name)) goto Finito;
            _count--;
            ListData Node = this._head;
            while (Node.Name != Name) Node = Node.Next;
            Node.Previous.SetNext(Node.Next);
            Node.Next.SetPrevious(Node.Previous);
        Finito:
            return _count;
        }
    }



    public List items = new List();
}

Edit: It’d be nice to get all entries on a single line… I’m going to look into it some more (just for phun)

Eratta: The above code doesn’t work too well if item names are the same; if you look at the code you will see why…

Can you upload picture of the result (to know if it’s look user-friendly in the inspector). Thank you really much for your time. But I found this : Tutorial to display a list which look very good on the inspector and works like the way I want (but certainly less efficient.

This is maybe better than what I excepted, I will certainly follow other tutorials on this website.

lol - very nice - nah - my code displayed it as a mess - I was trying to get it too look more like what you have - but ran out of time :wink:

I hope, this is the last time I answer to mylself :stuck_out_tongue:

For people who are always looking for dictionary visible in the inspector of Unity, there is a really cool package you can import :
https://bitbucket.org/rotorz/ordered-dictionary-asset-for-unity?utm_medium=referral&utm_campaign=ZEEF&utm_source=https%3A%2F%2Funity3d-open-source.zeef.com%2Ftaras.leskiv

The result is pretty good :

dictionary is generic, I choose to do an example with string key and sprite value but you can do anything else and stock info into Asset folder.

I do it all the time, so it’s definitely possible. No need for ScriptableObject or other weird stuff.

It’s one of the Advanced Inspector feature.

Is this a plugin from the asset store or something we need to enable in Unity ?

It’s on the Asset Store. My point being is, if an asset on the store can do it, it’s definitely possible do the same.