C# can't see Dictionary in Inspector
Hi everyone, is there a way to make a Dictionary visible in the inspector? I want to see the values of the dictionary while I run the program.
Answer by bompi88 · Dec 11, 2012 at 09:17 PM
No, I don't think so. Found something relevant on this site. Quoting: "Unity won't serialize Dictionary, however you could store a List<> for keys and a List<> for values, and sew them up in a non serialized dictionary on Awake(). This doesn't solve the problem of when you want to modify the dictionary and have it "saved" back, but it is a handy trick in a lot of other cases."
I have never tried, but I will suggest a workaround though. Maybe you could work around it with converting the Keys and Values to Lists:
public List<string> keys = new List<string>();
public List<MyClass> values = new List<MyClass>();
private Dictionary<string,MyClass> dict;
after adding to Dictionary, you could:
values.AddRange(dict.Values);
keys = dict.Keys.ToList();
Answer by Demigiant · Dec 11, 2012 at 09:05 PM
You could create your own Inspector and parse the dictionary values to show them. Other than that, the default inspector doesn't consider Dictionaries, since Unity can't serialize them.
Answer by Hayden_Steed · Nov 13, 2016 at 10:43 PM
A addition to the answer above me. I found a problem where if you want a key and a value with the public dictionary this does not work. For example:
Dictionary<string,string> names;
So in order to fix this problem you make you public list with a value of KeyValuePair so like this:
public List<KeyValuePair<string, string>> NAMES;
foreach (KeyValuePair<string,string> name in NAMES)
{
names.add(name.Key,name.Value);
}
Your answer
Follow this Question
Related Questions
How to create an if statement for each element within a list C# 2 Answers
C# Rigidbody Rotation Resets 1 Answer
How to make enemy chase player. Basic AI 7 Answers
Collider disabling by itself... 2 Answers
When to use a namespace? 2 Answers