SerializableDictionary.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. namespace UnityEditor.U2D.Animation
  6. {
  7. [Serializable]
  8. internal class SerializableDictionary<TKey, TValue> : IDictionary<TKey, TValue>, ISerializationCallbackReceiver
  9. {
  10. [SerializeField]
  11. private List<TKey> m_Keys;
  12. [SerializeField]
  13. private List<TValue> m_Values;
  14. private Dictionary<TKey, TValue> m_Dictionary = new Dictionary<TKey, TValue>();
  15. public TValue this[TKey key]
  16. {
  17. get { return m_Dictionary[key]; }
  18. set { m_Dictionary[key] = value; }
  19. }
  20. public TKey this[TValue value]
  21. {
  22. get
  23. {
  24. m_Keys = new List<TKey>(m_Dictionary.Keys);
  25. m_Values = new List<TValue>(m_Dictionary.Values);
  26. var index = m_Values.FindIndex(x => x.Equals(value));
  27. if (index < 0)
  28. throw new KeyNotFoundException();
  29. return m_Keys[index];
  30. }
  31. }
  32. public ICollection<TKey> Keys
  33. {
  34. get { return m_Dictionary.Keys; }
  35. }
  36. public ICollection<TValue> Values
  37. {
  38. get { return m_Dictionary.Values; }
  39. }
  40. public void Add(TKey key, TValue value)
  41. {
  42. m_Dictionary.Add(key, value);
  43. }
  44. public bool ContainsKey(TKey key)
  45. {
  46. return m_Dictionary.ContainsKey(key);
  47. }
  48. public bool Remove(TKey key)
  49. {
  50. return m_Dictionary.Remove(key);
  51. }
  52. public bool TryGetValue(TKey key, out TValue value)
  53. {
  54. return m_Dictionary.TryGetValue(key, out value);
  55. }
  56. public void Clear()
  57. {
  58. m_Dictionary.Clear();
  59. }
  60. public int Count
  61. {
  62. get { return m_Dictionary.Count; }
  63. }
  64. bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly
  65. {
  66. get { return (m_Dictionary as ICollection<KeyValuePair<TKey, TValue>>).IsReadOnly; }
  67. }
  68. void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item)
  69. {
  70. (m_Dictionary as ICollection<KeyValuePair<TKey, TValue>>).Add(item);
  71. }
  72. bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item)
  73. {
  74. return (m_Dictionary as ICollection<KeyValuePair<TKey, TValue>>).Contains(item);
  75. }
  76. void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
  77. {
  78. (m_Dictionary as ICollection<KeyValuePair<TKey, TValue>>).CopyTo(array, arrayIndex);
  79. }
  80. bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item)
  81. {
  82. return (m_Dictionary as ICollection<KeyValuePair<TKey, TValue>>).Remove(item);
  83. }
  84. IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator()
  85. {
  86. return (m_Dictionary as IEnumerable<KeyValuePair<TKey, TValue>>).GetEnumerator();
  87. }
  88. public IEnumerator GetEnumerator()
  89. {
  90. return m_Dictionary.GetEnumerator();
  91. }
  92. void ISerializationCallbackReceiver.OnBeforeSerialize()
  93. {
  94. m_Keys = new List<TKey>(m_Dictionary.Keys);
  95. m_Values = new List<TValue>(m_Dictionary.Values);
  96. }
  97. void ISerializationCallbackReceiver.OnAfterDeserialize()
  98. {
  99. Debug.Assert(m_Keys.Count == m_Values.Count);
  100. Clear();
  101. for (var i = 0; i < m_Keys.Count; ++i)
  102. Add(m_Keys[i], m_Values[i]);
  103. }
  104. }
  105. }