KeyframeGroupDictionary.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. namespace Funly.SkyStudio
  6. {
  7. [Serializable]
  8. public class KeyframeGroupDictionary : System.Object, ISerializationCallbackReceiver, IEnumerable<string>
  9. {
  10. [NonSerialized]
  11. private Dictionary<string, IKeyframeGroup> m_Groups = new Dictionary<string, IKeyframeGroup>();
  12. [SerializeField]
  13. private ColorGroupDictionary m_ColorGroup = new ColorGroupDictionary();
  14. [SerializeField]
  15. private NumberGroupDictionary m_NumberGroup = new NumberGroupDictionary();
  16. [SerializeField]
  17. private TextureGroupDictionary m_TextureGroup = new TextureGroupDictionary();
  18. [SerializeField]
  19. private SpherePointGroupDictionary m_SpherePointGroup = new SpherePointGroupDictionary();
  20. [SerializeField]
  21. private BoolGroupDictionary m_BoolGroup = new BoolGroupDictionary();
  22. public IKeyframeGroup this[string aKey]
  23. {
  24. get { return m_Groups[aKey]; }
  25. set { m_Groups[aKey] = value; }
  26. }
  27. public bool ContainsKey(string key)
  28. {
  29. return m_Groups.ContainsKey(key);
  30. }
  31. public void Clear()
  32. {
  33. m_Groups.Clear();
  34. }
  35. public T GetGroup<T>(string propertyName) where T : class
  36. {
  37. if (typeof(T) == typeof(ColorKeyframeGroup)) {
  38. return m_Groups[propertyName] as T;
  39. }
  40. if (typeof(T) == typeof(NumberKeyframeGroup)) {
  41. return m_Groups[propertyName] as T;
  42. }
  43. if (typeof(T) == typeof(TextureKeyframeGroup)) {
  44. return m_Groups[propertyName] as T;
  45. }
  46. if (typeof(T) == typeof(SpherePointGroupDictionary)) {
  47. return m_Groups[propertyName] as T;
  48. }
  49. if (typeof(T) == typeof(BoolKeyframeGroup)) {
  50. return m_Groups[propertyName] as T;
  51. }
  52. return null;
  53. }
  54. // Unity doesn't supporty polymorphic serialization, so we split into type safe lists.
  55. public void OnBeforeSerialize()
  56. {
  57. m_ColorGroup.Clear();
  58. m_NumberGroup.Clear();
  59. m_TextureGroup.Clear();
  60. m_SpherePointGroup.Clear();
  61. m_BoolGroup.Clear();
  62. foreach (string key in m_Groups.Keys) {
  63. IKeyframeGroup obj = m_Groups[key];
  64. if (obj is ColorKeyframeGroup) {
  65. m_ColorGroup[key] = obj as ColorKeyframeGroup;
  66. } else if (obj is NumberKeyframeGroup) {
  67. m_NumberGroup[key] = obj as NumberKeyframeGroup;
  68. } else if (obj is TextureKeyframeGroup) {
  69. m_TextureGroup[key] = obj as TextureKeyframeGroup;
  70. } else if (obj is SpherePointKeyframeGroup) {
  71. m_SpherePointGroup[key] = obj as SpherePointKeyframeGroup;
  72. } else if (obj is BoolKeyframeGroup) {
  73. m_BoolGroup[key] = obj as BoolKeyframeGroup;
  74. }
  75. }
  76. }
  77. // Restore the generic list that uses an interface group.
  78. public void OnAfterDeserialize()
  79. {
  80. m_Groups.Clear();
  81. foreach (string key in m_ColorGroup.dict.Keys) {
  82. m_Groups[key] = m_ColorGroup[key];
  83. }
  84. foreach (string key in m_NumberGroup.dict.Keys) {
  85. m_Groups[key] = m_NumberGroup[key];
  86. }
  87. foreach (string key in m_TextureGroup.dict.Keys) {
  88. m_Groups[key] = m_TextureGroup[key];
  89. }
  90. foreach (string key in m_SpherePointGroup.dict.Keys) {
  91. m_Groups[key] = m_SpherePointGroup[key];
  92. }
  93. foreach (string key in m_BoolGroup.dict.Keys) {
  94. m_Groups[key] = m_BoolGroup[key];
  95. }
  96. }
  97. public IEnumerator<string> GetEnumerator()
  98. {
  99. return m_Groups.Keys.GetEnumerator();
  100. }
  101. IEnumerator IEnumerable.GetEnumerator()
  102. {
  103. return GetEnumerator();
  104. }
  105. }
  106. }