internal static bool Duplicate(this TrackAsset track, PlayableDirector director, TimelineAsset destinationTimeline = null)
        {
            bool result;

            if (track == null)
            {
                result = false;
            }
            else
            {
                if (destinationTimeline == track.timelineAsset)
                {
                    destinationTimeline = null;
                }
                TimelineAsset timelineAsset = track.parent as TimelineAsset;
                TrackAsset    trackAsset    = track.parent as TrackAsset;
                if (timelineAsset == null && trackAsset == null)
                {
                    Debug.LogWarning("Cannot duplicate track because it is not parented to known type");
                    result = false;
                }
                else
                {
                    PlayableAsset playableAsset = destinationTimeline ?? track.parent;
                    TrackAsset    trackAsset2   = TimelineHelpers.Clone(playableAsset, track, director);
                    TrackExtensions.RecursiveSubtrackClone(track, trackAsset2, director);
                    Undo.RegisterCreatedObjectUndo(trackAsset2, "Duplicate");
                    TimelineCreateUtilities.SaveAssetIntoObject(trackAsset2, playableAsset);
                    TimelineUndo.PushUndo(playableAsset, "Duplicate");
                    if (destinationTimeline != null)
                    {
                        destinationTimeline.AddTrackInternal(trackAsset2);
                    }
                    else if (timelineAsset != null)
                    {
                        TrackExtensions.ReparentTracks(new List <TrackAsset>
                        {
                            trackAsset2
                        }, timelineAsset, track, false);
                    }
                    else
                    {
                        trackAsset.AddChildAfter(trackAsset2, track);
                    }
                    result = true;
                }
            }
            return(result);
        }
Beispiel #2
0
        internal static TrackAsset Duplicate(this TrackAsset track, PlayableDirector director,
                                             TimelineAsset destinationTimeline = null)
        {
            if (track == null)
            {
                return(null);
            }

            // if the destination is us, clear to avoid bad parenting (case 919421)
            if (destinationTimeline == track.timelineAsset)
            {
                destinationTimeline = null;
            }

            var timelineParent = track.parent as TimelineAsset;
            var trackParent    = track.parent as TrackAsset;

            if (timelineParent == null && trackParent == null)
            {
                Debug.LogWarning("Cannot duplicate track because it is not parented to known type");
                return(null);
            }

            // Determine who the final parent is. If we are pasting into another track, it's always the timeline.
            //  Otherwise it's the original parent
            PlayableAsset finalParent = destinationTimeline != null ? destinationTimeline : track.parent;

            // grab the list of tracks to generate a name from (923360) to get the list of names
            // no need to do this part recursively
            var finalTrackParent   = finalParent as TrackAsset;
            var finalTimelineAsset = finalParent as TimelineAsset;
            var otherTracks        = (finalTimelineAsset != null) ? finalTimelineAsset.trackObjects : finalTrackParent.subTracksObjects;

            // Important to create the new objects before pushing the original undo, or redo breaks the
            //  sequence
            var newTrack = TimelineHelpers.Clone(finalParent, track, director, finalParent);

            newTrack.name = TimelineCreateUtilities.GenerateUniqueActorName(otherTracks, newTrack.name);

            RecursiveSubtrackClone(track, newTrack, director, finalParent);
            TimelineUndo.RegisterCreatedObjectUndo(newTrack, "Duplicate");
            TimelineCreateUtilities.SaveAssetIntoObject(newTrack, finalParent);
            TimelineUndo.PushUndo(finalParent, "Duplicate");

            if (destinationTimeline != null) // other timeline
            {
                destinationTimeline.AddTrackInternal(newTrack);
            }
            else if (timelineParent != null) // this timeline, no parent
            {
                ReparentTracks(new List <TrackAsset> {
                    newTrack
                }, timelineParent, timelineParent.GetRootTracks().Last(), false);
            }
            else // this timeline, with parent
            {
                trackParent.AddChild(newTrack);
            }

            // Call the custom editor. this check prevents the call when copying to the clipboard
            if (destinationTimeline == null || destinationTimeline == TimelineEditor.inspectedAsset)
            {
                var customEditor = CustomTimelineEditorCache.GetTrackEditor(newTrack);
                try
                {
                    customEditor.OnCreate(newTrack, track);
                }
                catch (Exception e)
                {
                    Debug.LogException(e);
                }
            }

            return(newTrack);
        }
        internal static bool ReparentTracks(List <TrackAsset> tracksToMove, PlayableAsset targetParent, TrackAsset insertMarker, bool insertBefore)
        {
            TrackAsset    trackAsset    = targetParent as TrackAsset;
            TimelineAsset timelineAsset = targetParent as TimelineAsset;
            bool          result;

            if (tracksToMove == null || tracksToMove.Count == 0 || (trackAsset == null && timelineAsset == null))
            {
                result = false;
            }
            else
            {
                List <TrackAsset> list = (from x in tracksToMove
                                          where x.parent != targetParent
                                          select x).ToList <TrackAsset>();
                if (insertMarker == null && !list.Any <TrackAsset>())
                {
                    result = false;
                }
                else
                {
                    List <PlayableAsset> list2 = (from x in list
                                                  select x.parent into x
                                                  where x != null
                                                  select x).Distinct <PlayableAsset>().ToList <PlayableAsset>();
                    TimelineUndo.PushUndo(targetParent, "Reparent");
                    foreach (PlayableAsset current in list2)
                    {
                        TimelineUndo.PushUndo(current, "Reparent");
                    }
                    foreach (TrackAsset current2 in list)
                    {
                        TimelineUndo.PushUndo(current2, "Reparent");
                    }
                    foreach (TrackAsset current3 in list)
                    {
                        if (current3.parent != targetParent)
                        {
                            TrackAsset    trackAsset2    = current3.parent as TrackAsset;
                            TimelineAsset timelineAsset2 = current3.parent as TimelineAsset;
                            if (timelineAsset2 != null)
                            {
                                timelineAsset2.RemoveTrack(current3);
                            }
                            else if (trackAsset2 != null)
                            {
                                trackAsset2.RemoveSubTrack(current3);
                            }
                            if (trackAsset != null)
                            {
                                trackAsset.AddChild(current3);
                                trackAsset.SetCollapsed(false);
                            }
                            else
                            {
                                timelineAsset.AddTrackInternal(current3);
                            }
                        }
                    }
                    if (insertMarker != null)
                    {
                        List <TrackAsset> allTracks = (!(trackAsset != null)) ? timelineAsset.tracks : trackAsset.subTracks;
                        TimelineUtility.ReorderTracks(allTracks, tracksToMove, insertMarker, insertBefore);
                        if (insertMarker.timelineAsset != null)
                        {
                            insertMarker.timelineAsset.Invalidate();
                        }
                    }
                    result = true;
                }
            }
            return(result);
        }