Last week, I published an article about creating a Dictionary of Lists in Unity using Unity’s JavaScript. Some of the syntax is changed in C#, but the overall algorithm is exactly the same. I’ve already mentioned the underlying motive, so I’ll dive right in.
Here’s how to create a Dictionary of Lists in C# for Unity.
12345678910111213141516171819202122232425
Dictionary<string,List<int>>table=newDictionary<string,List<int>>();voidStart(){// Fill our table with Lists containing ints using Arrays to intialize// the Lists.table["a"]=newList<int>(){1};table["b"]=newList<int>(){2};table["c"]=newList<int>(){3};foreach(stringstrintable.Keys){// Get the value for our Key.List<int>value=table[str];// If the Key is the desired Key, append to its list.if(str=="c"){value.Add(4);}// print the first item in each of the Lists.Debug.Log(value[0]);}// Print the appended item to see that it worked.Debug.Log(table["c"][1]);}
These are the main differences between the Unity JavaScript and C# code. Take a look at the full file in a gist on github.