How to detect end of a timeline (C#)

Hello all :slight_smile:

I’m getting starting with Timeline, the new feature of Unity, and it’s working like a charm!

I’m trying to detect the end of a timeline. So far, I found this :

_director = transform.GetComponentInChildren<PlayableDirector>();

And this in a coroutine :

if (_director.playableGraph.IsDone())
{
    OnEnd();
}

Actually, it’s not working… (Object reference not set to an instance)

I’m looking for a more reliable way to do this. Do you know if there’s any events? Maybe this? But I don’t know how to get the PlayableBehaviour instance from the PlayableDirector.

Thanks a lot for your help!

[EDIT]

I found this way but I’m still not fully satisfied :

public IEnumerator CheckEnd()  
{
    while (Math.Abs(_director.duration - _director.time) > 0.2) 
    {  yield return new WaitForEndOfFrame();  }   
    OnEnd();   
 }  

public void OnEnd()   
{  
    Debug.Log("On end");  
}

As of 2018.1, you can listen for an event on the PlayableDirector:

I’ve been trying to solve this problem for a few hours and I solved it! You can try this code.

if (GetComponent<PlayableDirector>().state != PlayState.Playing)
{
    //your code
}

I hope it works.

Respect from Turkey :slight_smile:

Start this coroutine

private IEnumerator PlayTimelineRoutine(PlayableDirector playableDirector, Action onComplete)
{
    playableDirector.Play();
    yield return new WaitForSeconds((float) playableDirector.duration);
    onComplete();
}