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 List
s, 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 Sprite
s in someValues
by their key, not by a numerical index.