Search Unity

TimelineEditor.playableDirector stopped and played events & states bug

Discussion in 'Timeline' started by tealm, May 9, 2018.

  1. tealm

    tealm

    Joined:
    Feb 4, 2014
    Posts:
    108
    TimelineEditor.playableDirector doesn't properly fire stopped and played events.
    If you also log the TimelineEditor.playableDirector.state it will only show Playing after hitting the play button in the Timeline once.

    I reported this as a bug.

    Here's a simple script I wrote that shows the state and events firing only once.
    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEditor.Timeline;
    3.  
    4. using UnityEngine;
    5. using UnityEngine.Playables;
    6.  
    7. using System;
    8.  
    9. [InitializeOnLoad]
    10. class TimelineEditorEvents
    11. {
    12.     private static bool eventsAssigned = false;
    13.  
    14.     static TimelineEditorEvents ()
    15.     {
    16.         EditorApplication.update += Update;
    17.     }
    18.  
    19.  
    20.     static void Played(PlayableDirector playableDirector)
    21.     {
    22.         Debug.LogFormat("Played -> {0}, time: {1}", playableDirector.state, playableDirector.time);
    23.     }
    24.  
    25.  
    26.     static void Stopped(PlayableDirector playableDirector)
    27.     {
    28.         Debug.LogFormat("Stopped -> {0}, time: {1}", playableDirector.state, playableDirector.time);
    29.     }
    30.  
    31.     static void Update ()
    32.     {
    33.         if(TimelineEditor.playableDirector == null)
    34.             return;
    35.  
    36.         Debug.Log(TimelineEditor.playableDirector.state);
    37.  
    38.         if(!eventsAssigned)
    39.         {
    40.             Debug.Log("Events assigned!");
    41.             TimelineEditor.playableDirector.played += Played;
    42.             TimelineEditor.playableDirector.stopped += Stopped;
    43.             eventsAssigned = true;
    44.         }
    45.     }
    46. }
    Tested on Unity 2018.1
     
    Last edited: May 9, 2018
  2. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    In 2018.1 (and prior), the timeline editor doesn't actually play the timeline. It simulates playing by calling evaluate and advancing the time (through EditorApplicaton.update). The difference being a "playing" graph is time is advance by engine. So it's not expected that the events are accurate in editor.

    Admittedly, playableDirector.state does not reflect this difference particularly well. When the callbacks were added, the targeted use case was Playmode.

    In 2018.2, we added support to actually play graphs in Editor. Timeline editor now uses this when playing graphs and this should make those events fire as expected. (As a happy side note, it also allows us to play audio in editor).
     
  3. tealm

    tealm

    Joined:
    Feb 4, 2014
    Posts:
    108
    Thanks for the quick reply @seant_unity, great to hear you've solved this in 2018.2 as well as the audio track playback!