Без опису
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using UnityEngine.Playables;
  5. #if UNITY_EDITOR
  6. using UnityEditor;
  7. #endif
  8. namespace UnityEngine.Timeline
  9. {
  10. static class TimelineUndo
  11. {
  12. internal static bool undoEnabled
  13. {
  14. get
  15. {
  16. #if UNITY_EDITOR
  17. return DisableUndoGuard.enableUndo && DisableUndoScope.enableUndo;
  18. #else
  19. return false;
  20. #endif
  21. }
  22. }
  23. public static void PushDestroyUndo(TimelineAsset timeline, Object thingToDirty, Object objectToDestroy)
  24. {
  25. #if UNITY_EDITOR
  26. if (objectToDestroy == null || !undoEnabled)
  27. return;
  28. if (thingToDirty != null)
  29. EditorUtility.SetDirty(thingToDirty);
  30. if (timeline != null)
  31. EditorUtility.SetDirty(timeline);
  32. Undo.DestroyObjectImmediate(objectToDestroy);
  33. #else
  34. if (objectToDestroy != null)
  35. Object.Destroy(objectToDestroy);
  36. #endif
  37. }
  38. [Conditional("UNITY_EDITOR")]
  39. public static void PushUndo(Object[] thingsToDirty, string operation)
  40. {
  41. #if UNITY_EDITOR
  42. if (thingsToDirty == null || !undoEnabled)
  43. return;
  44. for (var i = 0; i < thingsToDirty.Length; i++)
  45. {
  46. if (thingsToDirty[i] is TrackAsset track)
  47. track.MarkDirtyTrackAndClips();
  48. EditorUtility.SetDirty(thingsToDirty[i]);
  49. }
  50. Undo.RegisterCompleteObjectUndo(thingsToDirty, UndoName(operation));
  51. #endif
  52. }
  53. [Conditional("UNITY_EDITOR")]
  54. public static void PushUndo(Object thingToDirty, string operation)
  55. {
  56. #if UNITY_EDITOR
  57. if (thingToDirty != null && undoEnabled)
  58. {
  59. var track = thingToDirty as TrackAsset;
  60. if (track != null)
  61. track.MarkDirtyTrackAndClips();
  62. EditorUtility.SetDirty(thingToDirty);
  63. Undo.RegisterCompleteObjectUndo(thingToDirty, UndoName(operation));
  64. }
  65. #endif
  66. }
  67. [Conditional("UNITY_EDITOR")]
  68. public static void RegisterCreatedObjectUndo(Object thingCreated, string operation)
  69. {
  70. #if UNITY_EDITOR
  71. if (undoEnabled)
  72. {
  73. Undo.RegisterCreatedObjectUndo(thingCreated, UndoName(operation));
  74. }
  75. #endif
  76. }
  77. internal static string UndoName(string name) => "Timeline " + name;
  78. #if UNITY_EDITOR
  79. /// <summary>
  80. /// Provides stack management of the undo state.
  81. /// </summary>
  82. internal struct DisableUndoGuard : IDisposable
  83. {
  84. internal static bool enableUndo = true;
  85. static readonly Stack<bool> m_UndoStateStack = new Stack<bool>();
  86. bool m_MustDispose;
  87. public DisableUndoGuard(bool disable)
  88. {
  89. m_MustDispose = true;
  90. m_UndoStateStack.Push(enableUndo);
  91. enableUndo = !disable;
  92. }
  93. public void Dispose()
  94. {
  95. if (m_MustDispose)
  96. {
  97. if (m_UndoStateStack.Count == 0)
  98. {
  99. Debug.LogError("UnMatched DisableUndoGuard calls");
  100. enableUndo = true;
  101. return;
  102. }
  103. enableUndo = m_UndoStateStack.Pop();
  104. m_MustDispose = false;
  105. }
  106. }
  107. }
  108. /// <summary>
  109. /// Provides an undo state switch.
  110. /// </summary>
  111. internal class DisableUndoScope : IDisposable
  112. {
  113. internal static bool enableUndo => m_Depth == 0;
  114. static int m_Depth;
  115. bool m_MustDispose;
  116. public DisableUndoScope()
  117. {
  118. m_MustDispose = true;
  119. m_Depth++;
  120. }
  121. public void Dispose()
  122. {
  123. if (m_MustDispose)
  124. {
  125. if (m_Depth == 0)
  126. {
  127. Debug.LogError("UnMatched DisableUndoScope calls");
  128. return;
  129. }
  130. m_Depth--;
  131. m_MustDispose = false;
  132. }
  133. }
  134. }
  135. #endif
  136. }
  137. }