Нет описания
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

TimelineClipExtensions.cs 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System;
  2. namespace UnityEngine.Timeline
  3. {
  4. /// <summary>
  5. /// Extension methods for TimelineClip
  6. /// </summary>
  7. public static class TimelineClipExtensions
  8. {
  9. static readonly string k_UndoSetParentTrackText = "Move Clip";
  10. /// <summary>
  11. /// Tries to move a TimelineClip to a different track. Validates that the destination track can accept the clip before performing the move.
  12. /// Fails if the clip's PlayableAsset is null, the current and destination tracks are the same or the destination track cannot accept the clip.
  13. /// </summary>
  14. /// <param name="clip">Clip that is being moved</param>
  15. /// <param name="destinationTrack">Desired destination track</param>
  16. /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="clip"/> or <paramref name="destinationTrack"/> are null</exception>
  17. /// <exception cref="System.InvalidOperationException">Thrown if the PlayableAsset in the TimelineClip is null</exception>
  18. /// <exception cref="System.InvalidOperationException">Thrown if the current parent track and destination track are the same</exception>
  19. /// <exception cref="System.InvalidOperationException">Thrown if the destination track cannot hold tracks of the given type</exception>
  20. public static void MoveToTrack(this TimelineClip clip, TrackAsset destinationTrack)
  21. {
  22. if (clip == null)
  23. {
  24. throw new ArgumentNullException($"'this' argument for {nameof(MoveToTrack)} cannot be null.");
  25. }
  26. if (destinationTrack == null)
  27. {
  28. throw new ArgumentNullException("Cannot move TimelineClip to a null track.");
  29. }
  30. TrackAsset parentTrack = clip.GetParentTrack();
  31. Object asset = clip.asset;
  32. // If the asset is null we cannot validate its type against the destination track
  33. if (asset == null)
  34. {
  35. throw new InvalidOperationException("Cannot move a TimelineClip to a different track if the TimelineClip's PlayableAsset is null.");
  36. }
  37. if (parentTrack == destinationTrack)
  38. {
  39. throw new InvalidOperationException($"TimelineClip is already on {destinationTrack.name}.");
  40. }
  41. if (!destinationTrack.ValidateClipType(asset.GetType()))
  42. {
  43. throw new InvalidOperationException($"Track {destinationTrack.name} cannot contain clips of type {clip.GetType().Name}.");
  44. }
  45. MoveToTrack_Impl(clip, destinationTrack, asset, parentTrack);
  46. }
  47. /// <summary>
  48. /// Tries to move a TimelineClip to a different track. Validates that the destination track can accept the clip before performing the move.
  49. /// Fails if the clip's PlayableAsset is null, the current and destination tracks are the same or the destination track cannot accept the clip.
  50. /// </summary>
  51. /// <param name="clip">Clip that is being moved</param>
  52. /// <param name="destinationTrack">Desired destination track</param>
  53. /// <returns>Returns true if the clip was successfully moved to the destination track, false otherwise. Also returns false if either argument is null</returns>
  54. /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="clip"/> or <paramref name="destinationTrack"/> are null</exception>
  55. public static bool TryMoveToTrack(this TimelineClip clip, TrackAsset destinationTrack)
  56. {
  57. if (clip == null)
  58. {
  59. throw new ArgumentNullException($"'this' argument for {nameof(TryMoveToTrack)} cannot be null.");
  60. }
  61. if (destinationTrack == null)
  62. {
  63. throw new ArgumentNullException("Cannot move TimelineClip to a null parent.");
  64. }
  65. TrackAsset parentTrack = clip.GetParentTrack();
  66. Object asset = clip.asset;
  67. // If the asset is null we cannot validate its type against the destination track
  68. if (asset == null)
  69. {
  70. return false;
  71. }
  72. if (parentTrack != destinationTrack)
  73. {
  74. if (!destinationTrack.ValidateClipType(asset.GetType()))
  75. {
  76. return false;
  77. }
  78. MoveToTrack_Impl(clip, destinationTrack, asset, parentTrack);
  79. return true;
  80. }
  81. return false;
  82. }
  83. static void MoveToTrack_Impl(TimelineClip clip, TrackAsset destinationTrack, Object asset, TrackAsset parentTrack)
  84. {
  85. TimelineUndo.PushUndo(asset, k_UndoSetParentTrackText);
  86. if (parentTrack != null)
  87. {
  88. TimelineUndo.PushUndo(parentTrack, k_UndoSetParentTrackText);
  89. }
  90. TimelineUndo.PushUndo(destinationTrack, k_UndoSetParentTrackText);
  91. clip.SetParentTrack_Internal(destinationTrack);
  92. if (parentTrack == null)
  93. {
  94. TimelineCreateUtilities.SaveAssetIntoObject(asset, destinationTrack);
  95. }
  96. else if (parentTrack.timelineAsset != destinationTrack.timelineAsset)
  97. {
  98. TimelineCreateUtilities.RemoveAssetFromObject(asset, parentTrack);
  99. TimelineCreateUtilities.SaveAssetIntoObject(asset, destinationTrack);
  100. }
  101. }
  102. }
  103. }