Cache.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. namespace UnityEditor.U2D.Animation
  6. {
  7. internal class Cache : BaseObject, ICacheUndo
  8. {
  9. public static T Create<T>() where T : Cache
  10. {
  11. var cache = CreateInstance<T>();
  12. cache.hideFlags = HideFlags.DontSave;
  13. return cache;
  14. }
  15. public static void Destroy(Cache cache)
  16. {
  17. cache.Destroy();
  18. DestroyImmediate(cache);
  19. }
  20. [SerializeField]
  21. private List<CacheObject> m_CacheObjects = new List<CacheObject>();
  22. [SerializeField]
  23. private List<CacheObject> m_RemovedCacheObjects = new List<CacheObject>();
  24. private string m_UndoOperationName = null;
  25. private IUndo m_DefaultUndo = new UnityEngineUndo();
  26. private IUndo m_UndoOverride = null;
  27. protected IUndo undo
  28. {
  29. get
  30. {
  31. if (undoOverride != null)
  32. return undoOverride;
  33. return m_DefaultUndo;
  34. }
  35. }
  36. public IUndo undoOverride
  37. {
  38. get { return m_UndoOverride; }
  39. set { m_UndoOverride = value; }
  40. }
  41. public bool isUndoOperationSet
  42. {
  43. get { return string.IsNullOrEmpty(m_UndoOperationName) == false; }
  44. }
  45. public void IncrementCurrentGroup()
  46. {
  47. undo.IncrementCurrentGroup();
  48. }
  49. public virtual void BeginUndoOperation(string operationName)
  50. {
  51. if (isUndoOperationSet == false)
  52. {
  53. Debug.Assert(!m_CacheObjects.Contains(null));
  54. m_UndoOperationName = operationName;
  55. undo.RegisterCompleteObjectUndo(this, m_UndoOperationName);
  56. undo.RegisterCompleteObjectUndo(m_CacheObjects.ToArray(), m_UndoOperationName);
  57. undo.RegisterCompleteObjectUndo(m_RemovedCacheObjects.ToArray(), m_UndoOperationName);
  58. }
  59. }
  60. public void EndUndoOperation()
  61. {
  62. m_UndoOperationName = null;
  63. }
  64. public bool IsRemoved(CacheObject cacheObject)
  65. {
  66. return m_RemovedCacheObjects.Contains(cacheObject);
  67. }
  68. public T CreateCache<T>() where T : CacheObject
  69. {
  70. var cacheObject = FindRemovedCacheObject<T>();
  71. if (cacheObject != null)
  72. {
  73. m_RemovedCacheObjects.Remove(cacheObject);
  74. cacheObject.OnEnable();
  75. }
  76. else
  77. {
  78. cacheObject = CacheObject.Create<T>(this);
  79. }
  80. m_CacheObjects.Add(cacheObject);
  81. cacheObject.OnCreate();
  82. return cacheObject;
  83. }
  84. private T FindRemovedCacheObject<T>() where T : CacheObject
  85. {
  86. return m_RemovedCacheObjects.FirstOrDefault((o) => o.GetType().Equals(typeof(T))) as T;
  87. }
  88. public void Destroy(CacheObject cacheObject)
  89. {
  90. Debug.Assert(cacheObject != null);
  91. Debug.Assert(cacheObject.owner == this);
  92. Debug.Assert(m_CacheObjects.Contains(cacheObject));
  93. m_CacheObjects.Remove(cacheObject);
  94. m_RemovedCacheObjects.Add(cacheObject);
  95. cacheObject.OnDestroy();
  96. }
  97. public void Destroy()
  98. {
  99. Debug.Assert(!m_CacheObjects.Contains(null));
  100. EndUndoOperation();
  101. undo.ClearUndo(this);
  102. var cacheObjects = m_CacheObjects.ToArray();
  103. foreach (var cacheObject in cacheObjects)
  104. DestroyImmediate(cacheObject);
  105. cacheObjects = m_RemovedCacheObjects.ToArray();
  106. foreach (var cacheObject in cacheObjects)
  107. DestroyImmediate(cacheObject);
  108. m_CacheObjects.Clear();
  109. m_RemovedCacheObjects.Clear();
  110. }
  111. }
  112. }