Search Unity

Reverse Timeline - Get Playable Instance from Playable Director Component

Discussion in 'Timeline' started by tcmeric, Jul 19, 2017.

  1. tcmeric

    tcmeric

    Joined:
    Dec 21, 2016
    Posts:
    190
    I am trying to reverse the timeline via a monobehavior script, rather than from within the timeline itself. I see there is an example script for the later. However, I am having problems getting the playable.

    How to get the instance of this playable from the timeline component?

    I tried something like this, but I know its not correct.

    ---
    Code (CSharp):
    1.  
    2. public PlayableDirector director = null;
    3. private PlayableAsset _playableAsset;
    4.  
    5. director = go.GetComponent<PlayableDirector>();
    6.  _playableAsset = director.playableAsset;
    7. _playableAsset .... ???
    8.  


    --
    Key Part of Original Code.

    Code (CSharp):
    1.  
    2.         void UpdateTime([B]Playable playable[/B])
    3.         {
    4.             double duration = Math.Max(0.1, director.playableAsset.duration);
    5.             var reverseTime = duration - playable.GetTime();
    6.             switch (director.extrapolationMode)
    7.             {
    8.                 case DirectorWrapMode.Hold:
    9.                     director.time = Math.Min(duration, Math.Max(0, reverseTime));
    10.                     break;
    11.                 case DirectorWrapMode.Loop:
    12.                    var loopedTime = Math.Max(0, playable.GetTime() % duration);
    13.                     var loopedReverseTime = duration - loopedTime;
    14.                     director.time = loopedReverseTime;
    15.                     break;
    16.                 case DirectorWrapMode.None:
    17.                     director.time = reverseTime;
    18.                     break;
    19.             }
    20.         }
    21.  
     
  2. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    Here's a small sample that may help. Stopping the director, and manually evaluating is one way of doing it. Note that audio won't play.

    Code (CSharp):
    1. [RequireComponent(typeof(PlayableDirector))]
    2. public class ReversePlayback : MonoBehaviour {
    3.     private PlayableDirector director;
    4.  
    5.     // Use this for initialization
    6.     void Start() {
    7.         director = GetComponent<PlayableDirector>();
    8.         director.Stop();
    9.         director.time = director.playableAsset.duration - 0.01;
    10.         director.Evaluate();
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     void Update() {
    15.         double t = director.time - Time.deltaTime;
    16.         if (t < 0)
    17.             t = 0;
    18.  
    19.         director.time = t;
    20.         director.Evaluate();
    21.  
    22.         if (t == 0) {
    23.             director.Stop();
    24.             enabled = false;
    25.         }
    26.     }
    27. }
     
    msl_manni and tcmeric like this.
  3. tcmeric

    tcmeric

    Joined:
    Dec 21, 2016
    Posts:
    190
    Thank Sean. I will give it a try. Much appreciated!
     
  4. Ziplock9000

    Ziplock9000

    Joined:
    Jan 26, 2016
    Posts:
    360
    How do you do this if you're not touching any Update code? You simply want to play the animation backwards the way it worked forwards without the need for code in Update()?
     
  5. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    Playing an animation backward in Timeline is not supported right now. It seems to be a highly requested feature though! The above code will play a whole timeline (animations included) backward, but it's a fake as the time is driven through the Update loop.
     
  6. Ziplock9000

    Ziplock9000

    Joined:
    Jan 26, 2016
    Posts:
    360
    I've noticed timeline does not support events/triggers either.
    I can't see how this new feature got out of beta when these two basic but frequently used functions are not available.
    I've just been singing it's praises on twitter and how it will revolutionise my development, but it seems it wont even work for many of the basic needs.
     
    tinmegali likes this.