Search Unity

Timeline set Time

Discussion in 'Timeline' started by Neonzone, Sep 10, 2018.

  1. Neonzone

    Neonzone

    Joined:
    Dec 14, 2017
    Posts:
    2
    Hi i want to set the played frame - the "time" of the timeline manually -

    as far i can control the slider in the timeline by using this code but the frame doesnt update correctly
    timeline update method gametime, play on awake is false

    hope you can help me
    Code (CSharp):
    1. public void setTime() { // gets called by my scrollbar
    2.      
    3.         Scrollbar Scrollbar = scroll.GetComponent<Scrollbar>();
    4.         PlayableDirector playableDirector = GetComponent<PlayableDirector>();
    5.  
    6.         float maxtime = 1;
    7.    
    8.         playableDirector.time = Scrollbar.value * maxtime;
    9.     }
     
    Last edited: Sep 11, 2018
    Neonzone_ likes this.
  2. Neonzone_

    Neonzone_

    Joined:
    Sep 12, 2018
    Posts:
    6
    Still looking for answers :( - or isnt it possible?
     
  3. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    playableDirector.Evaluate() is what your are looking for I believe.
     
    etliaojing and bab202 like this.
  4. Neonzone_

    Neonzone_

    Joined:
    Sep 12, 2018
    Posts:
    6
    thank you for the answer: for other readers - my final solution was
    Code (CSharp):
    1. private void setTime() {
    2.      
    3.         Scrollbar Scrollbar = scroll.GetComponent<Scrollbar>();
    4.         PlayableDirector playableDirector = GetComponent<PlayableDirector>();
    5.  
    6.         Debug.Log(Scrollbar.value);
    7.         float maxtime = 1;
    8.  
    9.         actTime = Scrollbar.value * maxtime;
    10.  
    11.         playableDirector.time = actTime;
    12.         playableDirector.RebuildGraph();
    13.         playableDirector.Play();
    14.         playableDirector.playableGraph.GetRootPlayable(0).SetSpeed(0);
    15.  
    16.     }
    i had to rebuild the graph because otherwise playableGraph.GetRootPlayable(0) finds no graph - maybe i didnt understand the concept but actually there is a timeline graph in the scene already - nevertheless by setting the speed to 0 and resetting the time i was able to control the time of a timeline manually
     
    aoikiwi, JG-Denver and seant_unity like this.
  5. bab202

    bab202

    Joined:
    Oct 18, 2018
    Posts:
    15
    It worked for me. Thanks!
     
  6. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
     
    florianveltman likes this.
  7. florianveltman

    florianveltman

    Joined:
    Sep 7, 2012
    Posts:
    27
    Hi @seant_unity sorry for the necro, just wanted to thank you for this additional note. I'm using a similar approach where I have the player "scrob" through a Timeline animation. I've been looking to solve a problem where Signal Emitters would fire off multiple times when set to fire retroactively. The addition of a
    playableDirector.playableGraph.IsValid() is false
    check made the difference, and spares me having to roll my own custom Signal Emitters, phew!

    Cheers.

    (Edit: turns out I now need to figure out a way of triggering Signal Emitters when moving backwards through the timeline though, as it seems that I was relying on the events continuously firing, to restore certain scene elements to a previous state...)
     
    Last edited: Sep 24, 2020
    seant_unity likes this.
  8. Pourya-MDP

    Pourya-MDP

    Joined:
    May 18, 2017
    Posts:
    145
    Hello dude is this snippest code for scrubbing timeline in runtime using a slider?
     
  9. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    That will work, but you should only rebuild the graph and start playing if playableDirector.playState == PlayState.Paused. Otherwise each time you call this method, you will reconstruct the entire timeline instance, which can be very costly.
     
    Pourya-MDP likes this.
  10. Pourya-MDP

    Pourya-MDP

    Joined:
    May 18, 2017
    Posts:
    145
    Hey
    Can you clearify this?
    Maybe telling a bit more , with some simple codes?
     
  11. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    Code (CSharp):
    1. private void setTime() {
    2.    
    3.         Scrollbar Scrollbar = scroll.GetComponent<Scrollbar>();
    4.         PlayableDirector playableDirector = GetComponent<PlayableDirector>();
    5.         Debug.Log(Scrollbar.value);
    6.         float maxtime = 1;
    7.         actTime = Scrollbar.value * maxtime;
    8.         if (playableDirector.state == PlayState.Paused)
    9.         {
    10.             // this will call RebuildGraph if needed
    11.             playableDirector.Play();
    12.            
    13.             // will set the speed of the graph to 0, so it's always playing but never
    14.             // advancing
    15.             playableDirector.playableGraph.GetRootPlayable(0).SetSpeed(0);
    16.         }
    17.        
    18.         playableDirector.time = actTime;
    19.  
    20.     }
     
    vonSchlank, JG-Denver and Pourya-MDP like this.
  12. Pourya-MDP

    Pourya-MDP

    Joined:
    May 18, 2017
    Posts:
    145
    Thank you @seant_unity
     
  13. Voronoi

    Voronoi

    Joined:
    Jul 2, 2012
    Posts:
    589
    I don't know if this is better or not, but I hooked up the slider directly to the setTime function and it worked well.

    Code (CSharp):
    1.    public PlayableDirector m_playable;
    2.  
    3.     public void setTime(Vector2 value)
    4.     {
    5.         //Invert slider value
    6.         float t = Mathf.Lerp(1, 0, value.y);
    7.         double maxtime = m_playable.duration;
    8.         double actTime = t * maxtime;
    9.         if (m_playable.state == PlayState.Paused)
    10.         {
    11.             // this will call RebuildGraph if needed
    12.             m_playable.Play();
    13.  
    14.             // will set the speed of the graph to 0, so it's always playing but never
    15.             // advancing
    16.             m_playable.playableGraph.GetRootPlayable(0).SetSpeed(0);
    17.         }
    18.  
    19.         if (actTime < 0)
    20.             actTime = 0;
    21.         if (actTime > maxtime)
    22.             actTime = maxtime;
    23.         m_playable.time = actTime;
    24.  
    25.     }
    26.