31 lines
832 B
C#
31 lines
832 B
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
[System.Serializable]
|
|
public class SerializableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, ISerializationCallbackReceiver
|
|
{
|
|
// THIS DOES NOT WORK IF KEY OR VALUE ARE NON-SERIALIZABLE TYPES
|
|
[SerializeField] private List<TKey> keys = new List<TKey>();
|
|
[SerializeField] private List<TValue> values = new List<TValue>();
|
|
|
|
public void OnBeforeSerialize()
|
|
{
|
|
keys.Clear();
|
|
values.Clear();
|
|
foreach (var pair in this)
|
|
{
|
|
keys.Add(pair.Key);
|
|
values.Add(pair.Value);
|
|
}
|
|
}
|
|
|
|
public void OnAfterDeserialize()
|
|
{
|
|
Clear();
|
|
int count = Mathf.Min(keys.Count, values.Count);
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
Add(keys[i], values[i]);
|
|
}
|
|
}
|
|
} |