效果展示
除了使用signal,timeline也可以通过自定义clip实现rewind回放
原理:
实现PlayableBehaviour的子类,重写ProcessFrame方法,参数中的Playable对象的GetDuration()和GetTime()可以获得当前clip的总时间和当前时间
如果time接近duration,那么设法取到playableDirector,将time减去当前clip已播放的GetTime()即可
下面是PlayableBehaviour核心代码:
`public class LoopClipBehavior:PlayableBehaviour
{
private PlayableDirector director { get; set; }
public override void ProcessFrame(Playable playable, FrameData info, object playerData)
{
double duration = playable.GetDuration();
double time = playable.GetTime();
if (Math.Abs(time- duration)<=0.1f)
{
var diff = playable.GetTime();
director.time -= diff;
}
base.ProcessFrame(playable, info, playerData);
}
public override void OnPlayableCreate(Playable playable)
{
base.OnPlayableCreate(playable);
director = (playable.GetGraph().GetResolver() as PlayableDirector);
}
}
`
同时也需要实现PlayableAsset和TrackAsset的子类
LoopClipTrack.cs
[TrackClipType(typeof(LoopClip))]
public class LoopClipTrack: TrackAsset
{
}
LoopClip.cs
`public class LoopClip : PlayableAsset
{
public LoopClipBehavior template = new LoopClipBehavior();
public override Playable CreatePlayable(PlayableGraph graph, GameObject owner)
{
var playable = ScriptPlayable<LoopClipBehavior>.Create(graph, template);
return playable;
}
}`