Unity开发笔记-Timeline利用Clip实现Rewind回放


效果展示

除了使用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;
}
  }`


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM