Trying to get percentage of the way through playable

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.

var _time = playable.GetTime();
var _duration = playable.GetDuration();
var _percentage = (float)( _time / _duration );

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.

1 Like

I found a work around. I overwrote the CreatePlayable method on the Track:

protected override Playable CreatePlayable(PlayableGraph graph, GameObject go, TimelineClip clip)
{
    var _playable = (ScriptPlayable<MyCustomBehaviour>)base.CreatePlayable( graph, go, clip );
    _playable.GetBehaviour().clip = clip;
    return _playable;
}

Now that the MyCustomBehaviour has a pointer to the clip on it, i can query the clip to get the clips duration.

var _time = inputPlayable.GetTime();
var _duration = inputPlayable.GetBehaviour().clip.duration;
var _percentage = (float)( _time / _duration );

I feel dirty. Excuse me while i take a shower.

4 Likes

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.