TrackExtensions.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Timeline;
  4. using UnityEngine.Playables;
  5. namespace UnityEngine.Timeline
  6. {
  7. /// <summary>
  8. /// Extension methods for TrackAssets
  9. /// </summary>
  10. public static class TrackAssetExtensions
  11. {
  12. /// <summary>
  13. /// Gets the GroupTrack this track belongs to.
  14. /// </summary>
  15. /// <param name="asset">The track asset to find the group of</param>
  16. /// <returns>The parent GroupTrack or null if the Track is an override track, or root track.</returns>
  17. public static GroupTrack GetGroup(this TrackAsset asset)
  18. {
  19. if (asset == null)
  20. return null;
  21. return asset.parent as GroupTrack;
  22. }
  23. /// <summary>
  24. /// Assigns the track to the specified group track.
  25. /// </summary>
  26. /// <param name="asset">The track to assign.</param>
  27. /// <param name="group">The GroupTrack to assign the track to.</param>
  28. /// <remarks>
  29. /// Does not support assigning to a group in a different timeline.
  30. /// </remarks>
  31. public static void SetGroup(this TrackAsset asset, GroupTrack group)
  32. {
  33. const string undoString = "Reparent";
  34. if (asset == null || asset == group || asset.parent == group)
  35. return;
  36. if (group != null && asset.timelineAsset != group.timelineAsset)
  37. throw new InvalidOperationException("Cannot assign to a group in a different timeline");
  38. TimelineUndo.PushUndo(asset, undoString);
  39. var timeline = asset.timelineAsset;
  40. var parentTrack = asset.parent as TrackAsset;
  41. var parentTimeline = asset.parent as TimelineAsset;
  42. if (parentTrack != null || parentTimeline != null)
  43. {
  44. TimelineUndo.PushUndo(asset.parent, undoString);
  45. if (parentTimeline != null)
  46. {
  47. parentTimeline.RemoveTrack(asset);
  48. }
  49. else
  50. {
  51. parentTrack.RemoveSubTrack(asset);
  52. }
  53. }
  54. if (group == null)
  55. {
  56. TimelineUndo.PushUndo(timeline, undoString);
  57. asset.parent = asset.timelineAsset;
  58. timeline.AddTrackInternal(asset);
  59. }
  60. else
  61. {
  62. TimelineUndo.PushUndo(group, undoString);
  63. group.AddChild(asset);
  64. }
  65. }
  66. }
  67. }