[System.Serializable]
public class SerializableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, ISerializationCallbackReceiver
{
[SerializeField]
private List<TKey> keys = new List<TKey>();
[SerializeField]
private List<TValue> values = new List<TValue>();
// save the dictionary to lists
public void OnBeforeSerialize()
{
keys.Clear();
values.Clear();
foreach (KeyValuePair<TKey, TValue> pair in this)
{
keys.Add(pair.Key);
values.Add(pair.Value);
}
}
// load dictionary from lists
public void OnAfterDeserialize()
{
this.Clear();
if (keys.Count != values.Count)
throw new Exception("there are " + keys.Count + " keys and " + values.Count + " values after deserialization. Make sure that both key and value types are serializable.");
for (int i = 0; i < keys.Count; i++)
this.Add(keys[i], values[i]);
}
}
μ΄ ν΄λμ€ λ§λ λ€μμ, Dictionary λμ μ SerializableDictionary μ¬μ©νμλ©΄ λ©λλ€.
κ·Έλ¦¬κ³ [SerializeField] private SerializableDictionary<string, int> μ κ°μ΄ μμ±νμλ©΄ μΈμ€νν° μ°½μ λμ
λλ¦¬κ° λΉλλΉ.
μ΄λ κ² μ§ ! μΈμ€νν°μ°½μ λμ
λλ¦¬κ° λμμ‘λ€μγ
γ
μμ μ€ν μ΄μ λ¬΄λ£ μμ μμ΄μ.