暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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