using System; using System.Collections; using System.Collections.Generic; using UnityEngine; namespace UnityEditor.U2D.Animation { [Serializable] internal class SerializableDictionary : IDictionary, ISerializationCallbackReceiver { [SerializeField] private List m_Keys; [SerializeField] private List m_Values; private Dictionary m_Dictionary = new Dictionary(); public TValue this[TKey key] { get { return m_Dictionary[key]; } set { m_Dictionary[key] = value; } } public TKey this[TValue value] { get { m_Keys = new List(m_Dictionary.Keys); m_Values = new List(m_Dictionary.Values); var index = m_Values.FindIndex(x => x.Equals(value)); if (index < 0) throw new KeyNotFoundException(); return m_Keys[index]; } } public ICollection Keys { get { return m_Dictionary.Keys; } } public ICollection Values { get { return m_Dictionary.Values; } } public void Add(TKey key, TValue value) { m_Dictionary.Add(key, value); } public bool ContainsKey(TKey key) { return m_Dictionary.ContainsKey(key); } public bool Remove(TKey key) { return m_Dictionary.Remove(key); } public bool TryGetValue(TKey key, out TValue value) { return m_Dictionary.TryGetValue(key, out value); } public void Clear() { m_Dictionary.Clear(); } public int Count { get { return m_Dictionary.Count; } } bool ICollection>.IsReadOnly { get { return (m_Dictionary as ICollection>).IsReadOnly; } } void ICollection>.Add(KeyValuePair item) { (m_Dictionary as ICollection>).Add(item); } bool ICollection>.Contains(KeyValuePair item) { return (m_Dictionary as ICollection>).Contains(item); } void ICollection>.CopyTo(KeyValuePair[] array, int arrayIndex) { (m_Dictionary as ICollection>).CopyTo(array, arrayIndex); } bool ICollection>.Remove(KeyValuePair item) { return (m_Dictionary as ICollection>).Remove(item); } IEnumerator> IEnumerable>.GetEnumerator() { return (m_Dictionary as IEnumerable>).GetEnumerator(); } public IEnumerator GetEnumerator() { return m_Dictionary.GetEnumerator(); } void ISerializationCallbackReceiver.OnBeforeSerialize() { m_Keys = new List(m_Dictionary.Keys); m_Values = new List(m_Dictionary.Values); } void ISerializationCallbackReceiver.OnAfterDeserialize() { Debug.Assert(m_Keys.Count == m_Values.Count); Clear(); for (var i = 0; i < m_Keys.Count; ++i) Add(m_Keys[i], m_Values[i]); } } }