Sin descripción
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.

TimelineUndo.cs 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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)
  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[] thingsToDirty, string operation)
  29. {
  30. #if UNITY_EDITOR
  31. if (thingsToDirty == null || !DisableUndoGuard.enableUndo)
  32. return;
  33. for (var i = 0; i < thingsToDirty.Length; i++)
  34. {
  35. if (thingsToDirty[i] is TrackAsset track)
  36. track.MarkDirty();
  37. EditorUtility.SetDirty(thingsToDirty[i]);
  38. }
  39. Undo.RegisterCompleteObjectUndo(thingsToDirty, UndoName(operation));
  40. #endif
  41. }
  42. [Conditional("UNITY_EDITOR")]
  43. public static void PushUndo(Object thingToDirty, string operation)
  44. {
  45. #if UNITY_EDITOR
  46. if (thingToDirty != null && DisableUndoGuard.enableUndo)
  47. {
  48. var track = thingToDirty as TrackAsset;
  49. if (track != null)
  50. track.MarkDirty();
  51. EditorUtility.SetDirty(thingToDirty);
  52. Undo.RegisterCompleteObjectUndo(thingToDirty, UndoName(operation));
  53. }
  54. #endif
  55. }
  56. [Conditional("UNITY_EDITOR")]
  57. public static void RegisterCreatedObjectUndo(Object thingCreated, string operation)
  58. {
  59. #if UNITY_EDITOR
  60. if (DisableUndoGuard.enableUndo)
  61. {
  62. Undo.RegisterCreatedObjectUndo(thingCreated, UndoName(operation));
  63. }
  64. #endif
  65. }
  66. private static string UndoName(string name) => "Timeline " + name;
  67. #if UNITY_EDITOR
  68. internal struct DisableUndoGuard : IDisposable
  69. {
  70. internal static bool enableUndo = true;
  71. static readonly Stack<bool> m_UndoStateStack = new Stack<bool>();
  72. bool m_Disposed;
  73. public DisableUndoGuard(bool disable)
  74. {
  75. m_Disposed = false;
  76. m_UndoStateStack.Push(enableUndo);
  77. enableUndo = !disable;
  78. }
  79. public void Dispose()
  80. {
  81. if (!m_Disposed)
  82. {
  83. if (m_UndoStateStack.Count == 0)
  84. {
  85. Debug.LogError("UnMatched DisableUndoGuard calls");
  86. enableUndo = true;
  87. return;
  88. }
  89. enableUndo = m_UndoStateStack.Pop();
  90. m_Disposed = true;
  91. }
  92. }
  93. }
  94. #endif
  95. }
  96. }