summaryrefslogtreecommitdiff
path: root/Library/PackageCache/com.unity.timeline@1.2.13/Runtime/Extensions/TrackExtensions.cs
blob: 87eec1bc23e371229fc4382befcc18aa63f1979b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System;
using UnityEngine;
using UnityEngine.Timeline;
using UnityEngine.Playables;

namespace UnityEngine.Timeline
{
    /// <summary>
    /// Extension methods for TrackAssets
    /// </summary>
    public static class TrackAssetExtensions
    {
        /// <summary>
        /// Gets the GroupTrack this track belongs to.
        /// </summary>
        /// <param name="asset">The track asset to find the group of</param>
        /// <returns>The parent GroupTrack or null if the Track is an override track, or root track.</returns>
        public static GroupTrack GetGroup(this TrackAsset asset)
        {
            if (asset == null)
                return null;

            return asset.parent as GroupTrack;
        }

        /// <summary>
        /// Assigns the track to the specified group track.
        /// </summary>
        /// <param name="asset">The track to assign.</param>
        /// <param name="group">The GroupTrack to assign the track to.</param>
        /// <remarks>
        /// Does not support assigning to a group in a different timeline.
        /// </remarks>
        public static void SetGroup(this TrackAsset asset, GroupTrack group)
        {
            const string undoString = "Reparent";

            if (asset == null || asset == group || asset.parent == group)
                return;

            if (group != null && asset.timelineAsset != group.timelineAsset)
                throw new InvalidOperationException("Cannot assign to a group in a different timeline");


            TimelineUndo.PushUndo(asset, undoString);

            var timeline = asset.timelineAsset;
            var parentTrack = asset.parent as TrackAsset;
            var parentTimeline = asset.parent as TimelineAsset;
            if (parentTrack != null || parentTimeline != null)
            {
                TimelineUndo.PushUndo(asset.parent, undoString);
                if (parentTimeline != null)
                {
                    parentTimeline.RemoveTrack(asset);
                }
                else
                {
                    parentTrack.RemoveSubTrack(asset);
                }
            }

            if (group == null)
            {
                TimelineUndo.PushUndo(timeline, undoString);
                asset.parent = asset.timelineAsset;
                timeline.AddTrackInternal(asset);
            }
            else
            {
                TimelineUndo.PushUndo(group, undoString);
                group.AddChild(asset);
            }
        }
    }
}