1

I read in some of the Unity documentation that a Dictionary isn't serializable so can't be modified from the editor. There was a hint to use two Lists, then use them to create the Dictionary:

Hint: 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().

However I don't seem to be able to find the documentation to do just that. So far I have the following:

public List<string> someKeys;
public List<Sprite> someValues;

private void Awake()
{
    someDictionary = new Dictionary<string, Sprite>();
}

someKeys and someValues are setup and populated in the Unity editor with the same number of elements. Naturally, element 0 of someKeys is the key for element 0 in someValues, as do the rest of the keys in order. How can I now use them both to create the keys and values in the someDictionary?

Update: I see from the duplicate marks that this has been answered using Linq, which as a newbie to C# I've not used yet, but certainly seems to do the job. But would this be safe in Unity, especially on devices like those running iOS? Additionally, would this method be better (i.e. faster and/or considered better practice than using a standard for loop?

Note: I want to access the Sprites in someValues by their key, not by a numerical index.

Bart
  • 19,692
  • 7
  • 68
  • 77
LeonardChallis
  • 7,759
  • 6
  • 45
  • 76
  • 1
    For each index n in the list add the n-th key and the n-th value to the dictionary. – Dirk Aug 25 '14 at 07:28
  • i think rather then a dictionary of two lists, a list of dictionary will be fine. – Ashok Damani Aug 25 '14 at 07:29
  • I've edited my question to provide more specifics that could affect it being marked as duplicated, with additional information on the solution I am trying to garner. – LeonardChallis Aug 25 '14 at 07:53

1 Answers1

3

The Dictionary class is a generic class with the first type argument corresponding to the type of the keys and the second type argument, the type of the values.

So the dictionary must look like: Dictionary<string, Sprite>

Then, you can add a (Key, Value) pair with the Dictionary.Add(key, value) method.

So here, you will have:

for (int numItem = 0; numItem < someKeys.Count; numItem++)
    dictionary.Add(someKeys[numItem], somevalues[numItem]);

But first make sure someKeys.Count == someValues.Count.

You could also use LINQ, but it appears that iOS may have limited support for LINQ (see Using LINQ when building to iOS - Unity Answers)

Cédric Bignon
  • 12,892
  • 3
  • 39
  • 51
  • I'm beginning to think that this would be a better solution than using Linq when developing for a multitude of platforms (i.e. iOS) from what I've ready about the support around the web. I'll keep an eye on any other updates - though don't expect much as it's been marked as duplicated. – LeonardChallis Aug 25 '14 at 07:52
  • @LeonardChallis Yes, this is exactly why I used dictionaries. Otherwise I'm a very big fan of LINQ :D And it's no more flagged as duplicated. – Cédric Bignon Aug 25 '14 at 07:57