How to know when a timeline is complete ?

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.

1 Like

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.

6 Likes

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.

2 Likes

Activate a scripted game object at the end your timeline to fire off that event.

1 Like

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.

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).

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 ?

1 Like

When the extrapolationMode is == DirectorWrapMode.Hold you can check if it’s complete by checking .time against .duration on the director.

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.

What we should really have is a timeline.onComplete += callback;

7 Likes

We are currently in the process of adding Actions (onPlay, onPause) to the PlayableDirector, so you can add your callbacks through the API.

1 Like

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?

I mean System.Action, sorry for the confusion :wink:

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?

Please add an OnComplete event which will be trigger when the timeline duration is reached, whatever the wrap mode is.

8 Likes

And onClipEnter and onClipExit

2 Likes

Is there a dedicated post where is it possible to follow what you are working on Timeline ?

1 Like

You can do onPlay += callback with System.Action. Should work just as well as events. :wink:

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.

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.

3 Likes