Skip to content

Instantly share code, notes, and snippets.

@olegmrzv
Last active February 19, 2023 19:54
Show Gist options
  • Save olegmrzv/d673afce11a61969a9cba572159e28c8 to your computer and use it in GitHub Desktop.
Save olegmrzv/d673afce11a61969a9cba572159e28c8 to your computer and use it in GitHub Desktop.
SerializedDictionary public version
[Serializable]
public class SerializedDictionary<K, V> : Dictionary<K, V>, ISerializationCallbackReceiver
{
[SerializeField]
private List<K> m_Keys = new List<K>();
[SerializeField]
private List<V> m_Values = new List<V>();
public void OnBeforeSerialize()
{
m_Keys.Clear();
m_Values.Clear();
foreach (var kvp in this)
{
m_Keys.Add(kvp.Key);
m_Values.Add(kvp.Value);
}
}
public void OnAfterDeserialize()
{
for (int i = 0; i < m_Keys.Count; i++)
Add(m_Keys[i], m_Values[i]);
m_Keys.Clear();
m_Values.Clear();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment