MarkerModifier.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. using UnityEngine.Timeline;
  6. using Object = UnityEngine.Object;
  7. namespace UnityEditor.Timeline
  8. {
  9. static class MarkerModifier
  10. {
  11. public static void DeleteMarker(IMarker marker)
  12. {
  13. var trackAsset = marker.parent;
  14. if (trackAsset != null)
  15. {
  16. SelectionManager.Remove(marker);
  17. trackAsset.DeleteMarker(marker);
  18. }
  19. }
  20. public static IEnumerable<IMarker> CloneMarkersToParent(IEnumerable<IMarker> markers, TrackAsset parent)
  21. {
  22. if (!markers.Any()) return Enumerable.Empty<IMarker>();
  23. var clonedMarkers = new List<IMarker>();
  24. foreach (var marker in markers)
  25. clonedMarkers.Add(CloneMarkerToParent(marker, parent));
  26. return clonedMarkers;
  27. }
  28. public static IMarker CloneMarkerToParent(IMarker marker, TrackAsset parent)
  29. {
  30. var markerObject = marker as ScriptableObject;
  31. if (markerObject == null) return null;
  32. var newMarkerObject = Object.Instantiate(markerObject);
  33. AddMarkerToParent(newMarkerObject, parent);
  34. newMarkerObject.name = markerObject.name;
  35. try
  36. {
  37. CustomTimelineEditorCache.GetMarkerEditor((IMarker)newMarkerObject).OnCreate((IMarker)newMarkerObject, marker);
  38. }
  39. catch (Exception e)
  40. {
  41. Debug.LogException(e);
  42. }
  43. return (IMarker)newMarkerObject;
  44. }
  45. static void AddMarkerToParent(ScriptableObject marker, TrackAsset parent)
  46. {
  47. TimelineCreateUtilities.SaveAssetIntoObject(marker, parent);
  48. TimelineUndo.RegisterCreatedObjectUndo(marker, "Duplicate Marker");
  49. TimelineUndo.PushUndo(parent, "Duplicate Marker");
  50. if (parent != null)
  51. {
  52. parent.AddMarker(marker);
  53. ((IMarker)marker).Initialize(parent);
  54. }
  55. }
  56. }
  57. }