Skip to content

Instantly share code, notes, and snippets.

@bellicapax
Created February 27, 2018 01:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bellicapax/9fb5f9c1468aa6d92e95426c0e9fb735 to your computer and use it in GitHub Desktop.
Save bellicapax/9fb5f9c1468aa6d92e95426c0e9fb735 to your computer and use it in GitHub Desktop.
An example implementation of a serializable Dictionary<string, int>
using UnityEngine;
namespace System.Collections.Generic
{
[Serializable]
public class StringIntTuple : SerializableKeyValuePair<string, int>
{
public StringIntTuple(string item1, int item2) : base(item1, item2) { }
}
[Serializable]
public class StringIntDictionary : SerializableDictionary<string, int>
{
[SerializeField] private List<StringIntTuple> _pairs = new List<StringIntTuple>();
protected override List<SerializableKeyValuePair<string, int>> _keyValuePairs
{
get
{
var list = new List<SerializableKeyValuePair<string, int>>();
foreach (var pair in _pairs)
{
list.Add(new SerializableKeyValuePair<string, int>(pair.Key, pair.Value));
}
return list;
}
set
{
_pairs.Clear();
foreach (var kvp in value)
{
_pairs.Add(new StringIntTuple(kvp.Key, kvp.Value));
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment