TimelineAsset_CreateRemove.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Timeline;
  5. using UnityEngine.Playables;
  6. using UnityEngineInternal; // for metro type extensions
  7. namespace UnityEngine.Timeline
  8. {
  9. public partial class TimelineAsset
  10. {
  11. /// <summary>
  12. /// Allows you to create a track and add it to the Timeline.
  13. /// </summary>
  14. /// <param name="type">The type of track to create. Must derive from TrackAsset.</param>
  15. /// <param name="parent">Track to parent to. This can be null.</param>
  16. /// <param name="name">Name to give the track.</param>
  17. /// <returns>The created track.</returns>
  18. /// <remarks>
  19. /// This method will throw an InvalidOperationException if the parent is not valid. The parent can be any GroupTrack, or a supported parent type of track. For example, this can be used to create override tracks in AnimationTracks.
  20. /// </remarks>
  21. public TrackAsset CreateTrack(Type type, TrackAsset parent, string name)
  22. {
  23. if (parent != null && parent.timelineAsset != this)
  24. throw new InvalidOperationException("Addtrack cannot parent to a track not in the Timeline");
  25. if (!typeof(TrackAsset).IsAssignableFrom(type))
  26. throw new InvalidOperationException("Supplied type must be a track asset");
  27. if (parent != null)
  28. {
  29. if (!TimelineCreateUtilities.ValidateParentTrack(parent, type))
  30. throw new InvalidOperationException("Cannot assign a child of type " + type.Name + " to a parent of type " + parent.GetType().Name);
  31. }
  32. var actualParent = parent != null ? parent as PlayableAsset : this;
  33. TimelineUndo.PushUndo(actualParent, "Create Track");
  34. var baseName = name;
  35. if (string.IsNullOrEmpty(baseName))
  36. {
  37. baseName = type.Name;
  38. #if UNITY_EDITOR
  39. baseName = UnityEditor.ObjectNames.NicifyVariableName(baseName);
  40. #endif
  41. }
  42. var trackName = baseName;
  43. if (parent != null)
  44. trackName = TimelineCreateUtilities.GenerateUniqueActorName(parent.subTracksObjects, baseName);
  45. else
  46. trackName = TimelineCreateUtilities.GenerateUniqueActorName(trackObjects, baseName);
  47. TrackAsset newTrack = AllocateTrack(parent, trackName, type);
  48. if (newTrack != null)
  49. {
  50. newTrack.name = trackName;
  51. TimelineCreateUtilities.SaveAssetIntoObject(newTrack, actualParent);
  52. }
  53. return newTrack;
  54. }
  55. /// <summary>
  56. /// Creates a track and adds it to the Timeline Asset.
  57. /// </summary>
  58. /// <param name="parent">Track to parent to. This can be null.</param>
  59. /// <param name="trackName">The name of the track being created.</param>
  60. /// <typeparam name="T">The type of track being created. The track type must be derived from TrackAsset.</typeparam>
  61. /// <returns>Returns the created track.</returns>
  62. /// <remarks>
  63. /// This method will throw an InvalidOperationException if the parent is not valid. The parent can be any GroupTrack, or a supported parent type of track. For example, this can be used to create override tracks in AnimationTracks.
  64. /// </remarks>
  65. public T CreateTrack<T>(TrackAsset parent, string trackName) where T : TrackAsset, new()
  66. {
  67. return (T)CreateTrack(typeof(T), parent, trackName);
  68. }
  69. /// <summary>
  70. /// Creates a track and adds it to the Timeline Asset.
  71. /// </summary>
  72. /// <param name="trackName">The name of the track being created.</param>
  73. /// <typeparam name="T">The type of track being created. The track type must be derived from TrackAsset.</typeparam>
  74. /// <returns>Returns the created track.</returns>
  75. public T CreateTrack<T>(string trackName) where T : TrackAsset, new()
  76. {
  77. return (T)CreateTrack(typeof(T), null, trackName);
  78. }
  79. /// <summary>
  80. /// Creates a track and adds it to the Timeline Asset.
  81. /// </summary>
  82. /// <typeparam name="T">The type of track being created. The track type must be derived from TrackAsset.</typeparam>
  83. /// <returns>Returns the created track.</returns>
  84. public T CreateTrack<T>() where T : TrackAsset, new()
  85. {
  86. return (T)CreateTrack(typeof(T), null, null);
  87. }
  88. /// <summary>
  89. /// Delete a clip from this timeline.
  90. /// </summary>
  91. /// <param name="clip">The clip to delete.</param>
  92. /// <returns>Returns true if the removal was successful</returns>
  93. /// <remarks>
  94. /// This method will delete a clip and any assets owned by the clip.
  95. /// </remarks>
  96. public bool DeleteClip(TimelineClip clip)
  97. {
  98. if (clip == null || clip.parentTrack == null)
  99. {
  100. return false;
  101. }
  102. if (this != clip.parentTrack.timelineAsset)
  103. {
  104. Debug.LogError("Cannot delete a clip from this timeline");
  105. return false;
  106. }
  107. TimelineUndo.PushUndo(clip.parentTrack, "Delete Clip");
  108. if (clip.curves != null)
  109. {
  110. TimelineUndo.PushDestroyUndo(this, clip.parentTrack, clip.curves, "Delete Curves");
  111. }
  112. // handle wrapped assets
  113. if (clip.asset != null)
  114. {
  115. DeleteRecordedAnimation(clip);
  116. // TODO -- we should flag assets and owned, instead of this check...
  117. #if UNITY_EDITOR
  118. string path = UnityEditor.AssetDatabase.GetAssetPath(clip.asset);
  119. if (path == UnityEditor.AssetDatabase.GetAssetPath(this))
  120. #endif
  121. {
  122. TimelineUndo.PushDestroyUndo(this, clip.parentTrack, clip.asset, "Delete Clip Asset");
  123. }
  124. }
  125. var clipParentTrack = clip.parentTrack;
  126. clipParentTrack.RemoveClip(clip);
  127. clipParentTrack.CalculateExtrapolationTimes();
  128. return true;
  129. }
  130. /// <summary>
  131. /// Deletes a track from a timeline, including all clips and subtracks.
  132. /// </summary>
  133. /// <param name="track">The track to delete. It must be owned by this Timeline.</param>
  134. /// <returns>True if the track was deleted successfully.</returns>
  135. public bool DeleteTrack(TrackAsset track)
  136. {
  137. if (track.timelineAsset != this)
  138. return false;
  139. // push before we modify properties
  140. TimelineUndo.PushUndo(track, "Delete Track");
  141. TimelineUndo.PushUndo(this, "Delete Track");
  142. TrackAsset parent = track.parent as TrackAsset;
  143. if (parent != null)
  144. TimelineUndo.PushUndo(parent, "Delete Track");
  145. var children = track.GetChildTracks();
  146. foreach (var child in children)
  147. {
  148. DeleteTrack(child);
  149. }
  150. DeleteRecordedAnimation(track);
  151. var clipsToDelete = new List<TimelineClip>(track.clips);
  152. foreach (var clip in clipsToDelete)
  153. {
  154. DeleteClip(clip);
  155. }
  156. RemoveTrack(track);
  157. TimelineUndo.PushDestroyUndo(this, this, track, "Delete Track");
  158. return true;
  159. }
  160. internal void MoveLastTrackBefore(TrackAsset asset)
  161. {
  162. if (m_Tracks == null || m_Tracks.Count < 2 || asset == null)
  163. return;
  164. var lastTrack = m_Tracks[m_Tracks.Count - 1];
  165. if (lastTrack == asset)
  166. return;
  167. for (int i = 0; i < m_Tracks.Count - 1; i++)
  168. {
  169. if (m_Tracks[i] == asset)
  170. {
  171. for (int j = m_Tracks.Count - 1; j > i; j--)
  172. m_Tracks[j] = m_Tracks[j - 1];
  173. m_Tracks[i] = lastTrack;
  174. Invalidate();
  175. break;
  176. }
  177. }
  178. }
  179. internal TrackAsset AllocateTrack(TrackAsset trackAssetParent, string trackName, Type trackType)
  180. {
  181. if (trackAssetParent != null && trackAssetParent.timelineAsset != this)
  182. throw new InvalidOperationException("Addtrack cannot parent to a track not in the Timeline");
  183. if (!typeof(TrackAsset).IsAssignableFrom(trackType))
  184. throw new InvalidOperationException("Supplied type must be a track asset");
  185. var asset = (TrackAsset)CreateInstance(trackType);
  186. asset.name = trackName;
  187. if (trackAssetParent != null)
  188. trackAssetParent.AddChild(asset);
  189. else
  190. AddTrackInternal(asset);
  191. return asset;
  192. }
  193. void DeleteRecordedAnimation(TrackAsset track)
  194. {
  195. var animTrack = track as AnimationTrack;
  196. if (animTrack != null && animTrack.infiniteClip != null)
  197. TimelineUndo.PushDestroyUndo(this, track, animTrack.infiniteClip, "Delete Track");
  198. if (track.curves != null)
  199. TimelineUndo.PushDestroyUndo(this, track, track.curves, "Delete Track Parameters");
  200. }
  201. void DeleteRecordedAnimation(TimelineClip clip)
  202. {
  203. if (clip == null)
  204. return;
  205. if (clip.curves != null)
  206. TimelineUndo.PushDestroyUndo(this, clip.parentTrack, clip.curves, "Delete Clip Parameters");
  207. if (!clip.recordable)
  208. return;
  209. AnimationPlayableAsset asset = clip.asset as AnimationPlayableAsset;
  210. if (asset == null || asset.clip == null)
  211. return;
  212. TimelineUndo.PushDestroyUndo(this, asset, asset.clip, "Delete Recording");
  213. }
  214. }
  215. }