Search Unity

How to know when a timeline is complete ?

Discussion in 'Timeline' started by Kiupe, Sep 4, 2017.

  1. Kiupe

    Kiupe

    Joined:
    Feb 1, 2013
    Posts:
    495
    Hi guys,

    When playing a timeline how to detect it has been completely played? I tried to compare the time and duration properties but when the timeline wrap property is set to "none" the "time" value will never be equal or greater than the "duration" value and will be reset to 0 when the timeline is completed.

    Does Unity consider to add event to that ? Because even if checking properties could work with other wrap mode it means that the code that want to know when the timeline is completed should be a Behavior in order to check in the Update method or call a Coroutines. I know it's possible to call Coroutines frome an other Behavior and for sure you could come with another solutions, but events would be very convenients.
     
  2. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    745
    You can check that the state of the PlayableDirector is not PlayState.Playing.

    We are looking at adding events to PlayableDirector to inform, at least, when a timeline is completed playing.
     
    Chambers88 and drTheMan like this.
  3. Kiupe

    Kiupe

    Joined:
    Feb 1, 2013
    Posts:
    495
    Ok, but when "wrap" property is set to "hold" the state stay forever to "Playing". So there is no "one" solutino to check that. Depending of the "wrap" property value you have to check the state or compare time and duration. Not realy convenient.
     
  4. Autarkis

    Autarkis

    Joined:
    Oct 10, 2011
    Posts:
    272
    Activate a scripted game object at the end your timeline to fire off that event.
     
  5. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    24,672
    As far as I know there is no guarantee that event will get fired. There's no guarantee an event will be processed. So this is not reliable with low framerate or hiccup from OS, etc.

    @seant_unity please correct me if I'm wrong.
     
  6. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    745
    You are correct. It sounds like the best course of action is a listener on the playable director that triggers when the playstate changes (completed), when the time stays the same (hold), or when the delta time goes backwards (loop).
     
  7. Kiupe

    Kiupe

    Joined:
    Feb 1, 2013
    Posts:
    495
    By the way, is it normal when the "wrap" property is set to "hold" the state is still "playing" ? I can understand why, it's like the last frame is "looping" but not sure why it's a good thing. Is cool to be able to maintain the last frame state but would it not be possible to do that in a "permanent" way that would prevent to let the director playing again and agin ?
     
    Carwashh likes this.
  8. Drapan

    Drapan

    Joined:
    Apr 9, 2013
    Posts:
    14
    When the extrapolationMode is == DirectorWrapMode.Hold you can check if it's complete by checking .time against .duration on the director.
     
  9. Kiupe

    Kiupe

    Joined:
    Feb 1, 2013
    Posts:
    495
    Yes, I know. But what bothers me here is that you have to check with different type of conditions depending on that mode. Not really intuitive nor easy. I really want a simple event that could be triggered when a timeline is complete.
     
  10. DGordon

    DGordon

    Joined:
    Dec 8, 2013
    Posts:
    342
    What we should really have is a timeline.onComplete += callback;
     
    rlegiedz, codestage, JonasLuz and 2 others like this.
  11. julienb

    julienb

    Unity Technologies

    Joined:
    Sep 9, 2016
    Posts:
    118
    We are currently in the process of adding Actions (onPlay, onPause) to the PlayableDirector, so you can add your callbacks through the API.
     
  12. DGordon

    DGordon

    Joined:
    Dec 8, 2013
    Posts:
    342
    Great. So long as we have a way of using code to hook into the exact moment it completes.

    Out of curiousity, what do you mean Actions? As in, System.Action? Or do you mean something more similar to UnityEvents?
     
  13. julienb

    julienb

    Unity Technologies

    Joined:
    Sep 9, 2016
    Posts:
    118
    I mean System.Action, sorry for the confusion ;)
     
  14. DGordon

    DGordon

    Joined:
    Dec 8, 2013
    Posts:
    342
    Does this mean only 1 action can be attached to onPlay? As in: timeline.onPlay = () => Debug.Log("do something");

    That works, but that means you can't just listen and unlisten for the event ... you need to make sure you're not stomping something else. Also, doesn't most of the Unity API use events?
     
    Last edited: Sep 27, 2017
  15. Kiupe

    Kiupe

    Joined:
    Feb 1, 2013
    Posts:
    495
    Please add an OnComplete event which will be trigger when the timeline duration is reached, whatever the wrap mode is.
     
  16. Carwashh

    Carwashh

    Joined:
    Jul 28, 2012
    Posts:
    327
    And onClipEnter and onClipExit
     
    Deeeds likes this.
  17. Kiupe

    Kiupe

    Joined:
    Feb 1, 2013
    Posts:
    495
    Is there a dedicated post where is it possible to follow what you are working on Timeline ?
     
    DGordon likes this.
  18. julienb

    julienb

    Unity Technologies

    Joined:
    Sep 9, 2016
    Posts:
    118
    You can do onPlay += callback with System.Action. Should work just as well as events. ;)
     
  19. DGordon

    DGordon

    Joined:
    Dec 8, 2013
    Posts:
    342
    Okay, that works then. Don't get me wrong, I use Actions and Funcs all the time, they're great ... but I'm still curious what the reason is behind using actions here instead of events? Since a lot of other Unity systems use events for this kind of functionality (ie: VideoPlayer), why is this being built differently? I would have thought consistency between different parts of the Unity API would have been enforced, especially when we're talking about the same thing from a user experience (hooking into an onComplete, or any other "event").

    Just wondering if there was something specific that warranted the difference, or its a preference of the team working on this.
     
  20. julienb

    julienb

    Unity Technologies

    Joined:
    Sep 9, 2016
    Posts:
    118
    Noted! I agree we should have a event for Timeline completion. I am not sure having onClipEnter/onClipExit events is the right solution, but we definitely need a robust way of knowing when a clip is playing or is finished.
     
    Deeeds and Kiupe like this.
  21. julienb

    julienb

    Unity Technologies

    Joined:
    Sep 9, 2016
    Posts:
    118
    Sorry, I didn't use the right terms in my previous post, my bad (I should have used the word delegate, not event). So, there will be a System.Action event in the PlayableDirector. You will be able to use the += and -= operators, but not the = operator, which is a good thing. We try to avoid creating new delegates in the scripting API, so this is why System.Action is used here.
     
    Deeeds likes this.
  22. DGordon

    DGordon

    Joined:
    Dec 8, 2013
    Posts:
    342
    Ohh. That makes much more sense now. I had a feeling something was off :p. That sounds great, thanks!
     
    julienb likes this.
  23. Greyborn

    Greyborn

    Joined:
    May 26, 2016
    Posts:
    26
    Hey all, just joining this thread and wanted to ask/confirm that if what is being discussed above would allow us to accomplish the use-cases mentioned in my original question here regarding skipping (and executing certain clips that had not yet played on skipping): https://forum.unity.com/threads/skipping-support.498236/

    Thank you for any clarity on this!
     
  24. Exeneva

    Exeneva

    Joined:
    Dec 7, 2013
    Posts:
    300
    Sorry to revive an old thread, but did a way to check the completion of a timeline iteration get added in 2017.3?
     
  25. edwon

    edwon

    Joined:
    Apr 24, 2011
    Posts:
    161
    Are the timeline complete events on the way? I'm desparate for this...
     
  26. zyzyx

    zyzyx

    Joined:
    Jul 9, 2012
    Posts:
    221
    If this feature is part of the timeline events:
    Timeline events?
     
  27. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    745
    The events on the playable director are available in 2018.1.
     
    codestage and zyzyx like this.
  28. popMark

    popMark

    Joined:
    Apr 14, 2013
    Posts:
    88
    Having trouble atm because playableDirector.state always returns Paused even when playing, is this a bug?
     
  29. JMA1

    JMA1

    Joined:
    May 2, 2018
    Posts:
    1
    I think i found a solution !
    By time keeping the PlayableDirector.time if >0.
    See if this works:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Playables;
    3.  
    4. public class TimelineController : MonoBehaviour {
    5.  
    6.     private double timeKeeper;
    7.     private PlayableDirector director;
    8.     public GameObject Intro, Level;
    9.     public bool ResetTimeline;
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.         director = GetComponent<PlayableDirector>();
    14.         director.Play();
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update () {
    19.         if (director.state==PlayState.Paused && (timeKeeper+Time.deltaTime)>=director.duration)
    20.         {
    21.             //Transition animation play!!
    22.             Intro.SetActive(false);
    23.             Level.SetActive(true);
    24.         }
    25.  
    26.         if (director.time>0)
    27.         {
    28.             timeKeeper = director.time;
    29.         }
    30.  
    31.         if (ResetTimeline)
    32.         {
    33.             // Reset button
    34.             ResetTimeline = false;
    35.             // Resets level objects
    36.             Intro.SetActive(true);
    37.             Level.SetActive(false);
    38.             // Resets time keeping
    39.             timeKeeper = 0d;
    40.             // Resets director
    41.             director.Stop();
    42.             director.time = 0;
    43.             // Starts the timeline
    44.             Start();
    45.         }
    46.     }
    47. }
     
  30. Kiupe

    Kiupe

    Joined:
    Feb 1, 2013
    Posts:
    495
    Do you plan to have an OnComplete event ?

    Thanks
     
  31. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    745
    No, but the stopped event will get called, unless it's holding or looping.
     
  32. Kiupe

    Kiupe

    Joined:
    Feb 1, 2013
    Posts:
    495
    MD_Reptile likes this.
  33. EwieElektro

    EwieElektro

    Joined:
    Feb 22, 2016
    Posts:
    12
    in hold mode, the stopped Event are not called. (latest unity Version). :/ curriosly after i stop the Editor-Player.
     
  34. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    745
    The stopped event is not called in Hold/Loop because the timeline isn't stopped, it's still playing. The playable director stopped event refers to timeline being stopped and releasing it's hold on any objects in the scene.
     
  35. Rodolinc

    Rodolinc

    Joined:
    Sep 23, 2013
    Posts:
    59
    So, what is the best way to know when a timeline has reached the end of its longest layer (timeline not moving)? Checking the time and duration or activating a "messenger" object? I lost some hours trying to find out why the stopped event wasnt triggering, only to find out the reason. I think you should tell about this on the documentation.
     
    Last edited: Jan 11, 2019
  36. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    745
    Either way works. With 2019.1 you can add a signal at the end of the timeline to notify it has reached the end, even if it's about to loop or hold.

    It's a good suggestion to add that to the documentation for the stopped event, we will look into doing that.
     
    Rodolinc likes this.
  37. Rodolinc

    Rodolinc

    Joined:
    Sep 23, 2013
    Posts:
    59
    Thanks I ended up doing the event solution for now, but Ill suggest the team upgrading to 2019.1
     
  38. Chambers88

    Chambers88

    Joined:
    Feb 25, 2018
    Posts:
    4
    I don't recommend this because the playableDirector state returns Paused if the window loses focus (or if the editor is paused), which will make your game behave as if the timeline has ended when you click outside the window.

    BTW @seant_unity can you confirm if this is intended behavior or a bug? I personally think it is a little weird (and dangerous) because normally we don't expect that pausing the editor would affect the game state.
     
  39. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    745
    I would have to classify it as intended behaviour, although you are right, it is little weird. It returns pause because the time isn't advancing. We really should have more states than Playing and Paused.