Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Can I automate additional steps for "Duplicate"?

Discussion in 'Timeline' started by Peter77, Jul 9, 2022.

  1. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,438
    I use Timeline to spawn an object and move it from one position to another. This requires two tracks in my setup: Activation and Animation Tracks.

    When I want to spawn another object that is doing the same thing, just at a different time, I duplicate these tracks, but also have to duplicate the GameObject in the Hierarchy and replace the references in the duplicated tracks to the new GameObject. These two steps (duplicate GameObject and replace references) I would like to automate.

    Can I hook into the Timeline context menu to add my own "Duplicate" menu item? Is there a better way to do it?

    Below you can find the problem described in a video, it's just a 2 minute watch.
     
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,438
  3. julienb

    julienb

    Unity Technologies

    Joined:
    Sep 9, 2016
    Posts:
    174
    I don't see a way to combine both steps (duplicate track + replace gameobject references) into one single operation.

    However, you can still add a custom menu entry to replace the binding of a track with a new gameobject. It would then be a two step process:
    1- duplicate track
    2- right-click on the new track, then select your custom action.

    Here's a basic action to replace the binding of a track. It should show up in the context menu when right-clicking on a track.

    Code (CSharp):
    1. [MenuEntry("Replace binding with new GameObject")]
    2. public class ReplaceBinding : TrackAction
    3. {
    4.     public override ActionValidity Validate(IEnumerable<TrackAsset> tracks)
    5.     {
    6.         return ActionValidity.Valid;
    7.     }
    8.  
    9.     public override bool Execute(IEnumerable<TrackAsset> tracks)
    10.     {
    11.         foreach (TrackAsset track in tracks)
    12.         {
    13.             var newGameObject = new GameObject();
    14.             Undo.RegisterCreatedObjectUndo(newGameObject, "Replace binding");
    15.  
    16.             PlayableDirector director = TimelineEditor.inspectedDirector;
    17.             Undo.RecordObject(director, "Replace binding");
    18.             director.SetGenericBinding(track, newGameObject);
    19.         }
    20.  
    21.         return true;
    22.     }
    23.  
    24.     //Uncomment this method to assign a shortcut to your action
    25.     /*[TimelineShortcut("Replace binding", KeyCode.B)]
    26.     public static void HandleShortCut(ShortcutArguments args)
    27.     {
    28.         Invoker.InvokeWithSelectedTracks<ReplaceBinding>();
    29.     }*/
    30. }
    upload_2022-7-18_13-52-7.png