TimelineUndo.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. public static void PushDestroyUndo(TimelineAsset timeline, Object thingToDirty, Object objectToDestroy, string operation)
  13. {
  14. #if UNITY_EDITOR
  15. if (objectToDestroy == null || !DisableUndoGuard.enableUndo)
  16. return;
  17. if (thingToDirty != null)
  18. EditorUtility.SetDirty(thingToDirty);
  19. if (timeline != null)
  20. EditorUtility.SetDirty(timeline);
  21. Undo.DestroyObjectImmediate(objectToDestroy);
  22. #else
  23. if (objectToDestroy != null)
  24. Object.Destroy(objectToDestroy);
  25. #endif
  26. }
  27. [Conditional("UNITY_EDITOR")]
  28. public static void PushUndo(Object thingToDirty, string operation)
  29. {
  30. #if UNITY_EDITOR
  31. if (thingToDirty != null && DisableUndoGuard.enableUndo)
  32. {
  33. var track = thingToDirty as TrackAsset;
  34. if (track != null)
  35. track.MarkDirty();
  36. EditorUtility.SetDirty(thingToDirty);
  37. Undo.RegisterCompleteObjectUndo(thingToDirty, "Timeline " + operation);
  38. }
  39. #endif
  40. }
  41. [Conditional("UNITY_EDITOR")]
  42. public static void RegisterCreatedObjectUndo(Object thingCreated, string operation)
  43. {
  44. #if UNITY_EDITOR
  45. if (DisableUndoGuard.enableUndo)
  46. {
  47. Undo.RegisterCreatedObjectUndo(thingCreated, "Timeline " + operation);
  48. }
  49. #endif
  50. }
  51. #if UNITY_EDITOR
  52. public struct DisableUndoGuard : IDisposable
  53. {
  54. internal static bool enableUndo = true;
  55. static readonly Stack<bool> m_UndoStateStack = new Stack<bool>();
  56. bool m_Disposed;
  57. public DisableUndoGuard(bool disable)
  58. {
  59. m_Disposed = false;
  60. m_UndoStateStack.Push(enableUndo);
  61. enableUndo = !disable;
  62. }
  63. public void Dispose()
  64. {
  65. if (!m_Disposed)
  66. {
  67. if (m_UndoStateStack.Count == 0)
  68. {
  69. Debug.LogError("UnMatched DisableUndoGuard calls");
  70. enableUndo = true;
  71. return;
  72. }
  73. enableUndo = m_UndoStateStack.Pop();
  74. m_Disposed = true;
  75. }
  76. }
  77. }
  78. #endif
  79. }
  80. }