generated from max/template-unity-project
SerializationUtility
- Split local editor data into separate SO - Serializable dicts, sorted lists and sorted sets - Only delete and save loaded partitions
This commit is contained in:
101
Runtime/SerializationUtility.cs
Normal file
101
Runtime/SerializationUtility.cs
Normal file
@ -0,0 +1,101 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace VertexColor.ScenePartition
|
||||
{
|
||||
[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>();
|
||||
|
||||
public void OnBeforeSerialize()
|
||||
{
|
||||
keys.Clear();
|
||||
values.Clear();
|
||||
|
||||
foreach (KeyValuePair<TKey, TValue> pair in this)
|
||||
{
|
||||
keys.Add(pair.Key);
|
||||
values.Add(pair.Value);
|
||||
}
|
||||
}
|
||||
|
||||
public void OnAfterDeserialize()
|
||||
{
|
||||
Clear();
|
||||
|
||||
if (keys.Count != values.Count)
|
||||
{
|
||||
throw new Exception("Error: Key and value count does not match in the dictionary.");
|
||||
}
|
||||
|
||||
for (int i = 0; i < keys.Count; i++)
|
||||
{
|
||||
Add(keys[i], values[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class SerializableSortedList<TKey, TValue> : SortedList<TKey, TValue>, ISerializationCallbackReceiver
|
||||
{
|
||||
[SerializeField]
|
||||
private List<TKey> keys = new List<TKey>();
|
||||
[SerializeField]
|
||||
private List<TValue> values = new List<TValue>();
|
||||
|
||||
public void OnBeforeSerialize()
|
||||
{
|
||||
keys.Clear();
|
||||
values.Clear();
|
||||
|
||||
foreach (KeyValuePair<TKey, TValue> pair in this)
|
||||
{
|
||||
keys.Add(pair.Key);
|
||||
values.Add(pair.Value);
|
||||
}
|
||||
}
|
||||
|
||||
public void OnAfterDeserialize()
|
||||
{
|
||||
Clear();
|
||||
|
||||
if (keys.Count != values.Count)
|
||||
{
|
||||
throw new Exception("Error: Key and value count does not match in the dictionary.");
|
||||
}
|
||||
|
||||
for (int i = 0; i < keys.Count; i++)
|
||||
{
|
||||
Add(keys[i], values[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class SerializableSortedSet<T> : SortedSet<T>, ISerializationCallbackReceiver
|
||||
{
|
||||
[SerializeField]
|
||||
private List<T> elements = new List<T>();
|
||||
|
||||
public void OnBeforeSerialize()
|
||||
{
|
||||
elements.Clear();
|
||||
elements.AddRange(this);
|
||||
}
|
||||
|
||||
public void OnAfterDeserialize()
|
||||
{
|
||||
Clear();
|
||||
|
||||
for (int i = 0; i < elements.Count; i++)
|
||||
{
|
||||
Add(elements[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
11
Runtime/SerializationUtility.cs.meta
Normal file
11
Runtime/SerializationUtility.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d20cd39f6cd10914188acde7ee871339
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user