ScenePartition/Runtime/SerializationUtility.cs

96 lines
2.5 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using UnityEngine;
namespace VertexColor.ScenePartition
{
[System.Serializable]
public class SerializableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, ISerializationCallbackReceiver
{
[SerializeField]
private List<TKey> dictionaryKeys = new List<TKey>();
[SerializeField]
private List<TValue> dictionaryValues = new List<TValue>();
public void OnBeforeSerialize()
{
dictionaryKeys.Clear();
dictionaryValues.Clear();
foreach (KeyValuePair<TKey, TValue> item in this)
{
dictionaryKeys.Add(item.Key);
dictionaryValues.Add(item.Value);
}
}
public void OnAfterDeserialize()
{
Clear();
if (dictionaryKeys.Count != dictionaryValues.Count) return;
for (int i = 0; i < dictionaryKeys.Count; i++)
{
Add(dictionaryKeys[i], dictionaryValues[i]);
}
}
}
[System.Serializable]
public class SerializableSortedList<TKey, TValue> : SortedList<TKey, TValue>, ISerializationCallbackReceiver
{
[SerializeField]
private List<TKey> sortedListKeys = new List<TKey>();
[SerializeField]
private List<TValue> sortedListValues = new List<TValue>();
public void OnBeforeSerialize()
{
sortedListKeys.Clear();
sortedListValues.Clear();
foreach (KeyValuePair<TKey, TValue> item in this)
{
sortedListKeys.Add(item.Key);
sortedListValues.Add(item.Value);
}
}
public void OnAfterDeserialize()
{
Clear();
if (sortedListKeys.Count != sortedListValues.Count) return;
for (int i = 0; i < sortedListKeys.Count; i++)
{
Add(sortedListKeys[i], sortedListValues[i]);
}
}
}
[System.Serializable]
public class SerializableSortedSet<T> : SortedSet<T>, ISerializationCallbackReceiver
{
[SerializeField]
private List<T> sortedSetValues = new List<T>();
public void OnBeforeSerialize()
{
sortedSetValues.Clear();
sortedSetValues.AddRange(this);
}
public void OnAfterDeserialize()
{
Clear();
for (int i = 0; i < sortedSetValues.Count; i++)
{
Add(sortedSetValues[i]);
}
}
}
}