Search Unity

Scrubbing a timeline in code during play mode

Discussion in 'Timeline' started by Aaron-Meyers, Oct 30, 2018.

  1. Aaron-Meyers

    Aaron-Meyers

    Joined:
    Dec 8, 2009
    Posts:
    305
    I'm trying to manually scrub a timeline from a script by setting PlayableDirector.time. I have a Timeline panel open in the editor and I can see the playhead moving around properly when I do this, but none of the tracks appear to update.

    If I grab the playhead in the timeline and move it around in there, the game objects update as expected, but not when I set it from the script. I tried setting the timeUpdateMode to Manual and calling PlayableDirector.Play() after setting the time, but it doesn't seem to do anything.

    The documentation says that when using Manual update mode, "You need to manually call PlayerController.Tick with your own deltaTime", but I don't actually have any idea what PlayerController is and doesn't appear to be a member variable of PlayableDirector. I might be chasing down the wrong path with this anyway.

    I guess I just need to know how to force a DirectorPlayable to update everything based on a manually set time.
     
  2. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    You will need to do playableDirector.Evaluate(); each frame you want to update it, which is slower than regular playback.

    Or, a bit of a workaround, you could do
    playableDirector.timeUpdateMode = DirectorUpdateMode.GameTime; // or anything other than manual
    playableDirector.RebuildPlayableGraph();
    playableDirector.playableGraph.GetRootPlayable(0).SetSpeed(0); // prevent time from advancing
    playableDirector.Play();

    Then it will evaluate everyframe and you can change the time at will.

    In Manual mode, the update doesn't occur - not sure if it's a bug or a feature.... this is a workaround to get the behaviour you expect.
     
  3. halvorss

    halvorss

    Joined:
    Mar 18, 2013
    Posts:
    10
    Was this in regards to having something like a slider and being able to scrub forward and backwards through a Timeline play sequence? Because that's something I'm looking to do, but cannot find good information or references on whether it even works. As you mentioned, in the Editor I can scrub the Timeline window playhead and watch the animations/sequences happen, but I need the user to be able to have such control. It can be done with just the Animator control, but then it looses all the other benefits from the Timeline setup such as turning object visibility on and off and syncing with audio effects. Thank you.
     
  4. KennethVedder

    KennethVedder

    Joined:
    Aug 29, 2012
    Posts:
    12

    Hey man, I was just dealing with this and found the answer here https://forum.unity.com/threads/scrubbing-a-timeline-in-code-during-play-mode.576412/

    I
    t works exactly as proposed :D
     
  5. elfasito

    elfasito

    Joined:
    Jul 4, 2017
    Posts:
    51
  6. KennethVedder

    KennethVedder

    Joined:
    Aug 29, 2012
    Posts:
    12
    Hey there, I think I did link the wrong post. Here's the solution: tmscb.png