Last active
February 19, 2023 19:54
-
-
Save olegmrzv/d673afce11a61969a9cba572159e28c8 to your computer and use it in GitHub Desktop.
SerializedDictionary public version
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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