// Serialization.cs
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace Funique
{
///
/// Helping serializating the array or list varaible
/// ------------------------------------------------
/// 幫助序列化 陣列或列表 的資料型態
///
/// Data type
[Serializable]
public class Serialization
{
[SerializeField] List target;
public List ToList() { return target; }
public T[] ToArray() { return target.ToArray(); }
public Serialization(List target)
{
this.target = target;
}
public Serialization(T[] target)
{
this.target = target.ToList();
}
}
///
/// Helping serializating the dictionary varaible
/// ------------------------------------------------
/// 幫助序列化 目錄 的資料型態
///
/// Data type
[Serializable]
public class Serialization : ISerializationCallbackReceiver
{
[SerializeField] List keys;
[SerializeField] List values;
Dictionary target;
public Dictionary ToDictionary() { return target; }
public Serialization(Dictionary target)
{
this.target = target;
}
public void OnBeforeSerialize()
{
keys = new List(target.Keys);
values = new List(target.Values);
}
public void OnAfterDeserialize()
{
var count = Math.Min(keys.Count, values.Count);
target = new Dictionary(count);
for (var i = 0; i < count; ++i)
{
target.Add(keys[i], values[i]);
}
}
}
}