Search Unity

Trying to get percentage of the way through playable

Discussion in 'Timeline' started by Steamroller, Nov 8, 2017.

  1. Steamroller

    Steamroller

    Joined:
    Jan 2, 2013
    Posts:
    71
    I'm using the following code inside my mixer to try and get the percentage of the way through a clip the timeline is current sitting.

    Code (CSharp):
    1.  
    2. var _time = playable.GetTime();
    3. var _duration = playable.GetDuration();
    4. var _percentage = (float)( _time / _duration );
    5.  
    This works for the most part. The problem arises whenever i set the Post-Extrapolate to something other than None. If the clip is the only clip on the track, the duration suddenly has 100,000 added to its playable duration. If there is more than one clip on the timeline, all but the last clip have a duration which includes the space between the clips. Confused yet? Sorry.

    I simply wish there was a method called playable.GetTimeNormalized() or something of that nature. If you could shed some light on how to achieve the desired behavior, it would be awesome.
     
    Meatloaf4 likes this.
  2. Steamroller

    Steamroller

    Joined:
    Jan 2, 2013
    Posts:
    71
    I found a work around. I overwrote the CreatePlayable method on the Track:

    Code (CSharp):
    1.  
    2. protected override Playable CreatePlayable(PlayableGraph graph, GameObject go, TimelineClip clip)
    3. {
    4.     var _playable = (ScriptPlayable<MyCustomBehaviour>)base.CreatePlayable( graph, go, clip );
    5.     _playable.GetBehaviour().clip = clip;
    6.     return _playable;
    7. }
    8.  
    Now that the MyCustomBehaviour has a pointer to the clip on it, i can query the clip to get the clips duration.

    Code (CSharp):
    1.  
    2. var _time = inputPlayable.GetTime();
    3. var _duration = inputPlayable.GetBehaviour().clip.duration;
    4. var _percentage = (float)( _time / _duration );
    5.  
    I feel dirty. Excuse me while i take a shower.
     
  3. LasseWestmark

    LasseWestmark

    Joined:
    Mar 18, 2014
    Posts:
    12
    Just had this same problem and I'm sure this will solve the problem. BUT:

    In my case I found it smarter to change my thinking. I wanted the ability to lerp say a color on a material over the course of the clip length.

    I ended deciding that this was working against how the timeline works in general. Now I'm just blending between clips by dragging them on top of each other, and the overall complexity for the user has gone down.

    So I guess this is just a reminder to step back and see if what you want is already solved by something else.
    Not saying that you could do without the percentage in your instance tho.