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


    先来看下效果,播放的结尾后回到前面的某一个位置,然后循环

    具体实现思路如下:

    1.设置marker标记,作为定位
    2.利用signal emitter,触发消息事件
    3.signal receiever监听事件,通过查找marker获得time
    4.通过设置PlayableDirector的time属性,改变timeline的播放位置,从而实现了rewind播放功能

    示例中,在timeline的结束处,在signal track上放置一个signal emitter
    点击signal emitter,添加signal asset,这里命名为marker_rewind

    在场景中新建一个signal reciever,新建MarkerRecieverRewind.cs脚本进行监听

    MarkerRecieverRewind需要实现INotificationReceiver接口

      `public void OnNotify(Playable origin, INotification notification, object context)
        {}`
    

    当监听到signal事件之后,我们需要
    1.获得marker对象,拿到time属性
    2.获得PlayableDirector,根据marker的time修改time属性

    marker的获取:

    1.将notification强转为signalEmitter

    SignalEmitter emitter = (SignalEmitter)notification;

    2.拿到TimlineAsset

    TimelineAsset ta = emitter.parent.timelineAsset;

    3,通过TimlineAsset的markerTrack属性获取marker

    IMarker marker = ta.markerTrack.GetMarker(0);

    PlayableDirector的获取:

    通过接口的Playable对象可以获取到

    PlayableDirector director = (PlayableDirector)origin.GetGraph().GetResolver();

    完整代码如下:

      `public class MarkerRecieverRewind : MonoBehaviour, INotificationReceiver
    {
        public void OnNotify(Playable origin, INotification notification, object context)
        {
            PlayableDirector director = (PlayableDirector)origin.GetGraph().GetResolver();
    
            SignalEmitter emitter = (SignalEmitter)notification;
            TimelineAsset ta = emitter.parent.timelineAsset;
            int markerCount = ta.markerTrack.GetMarkerCount();
            if (markerCount > 0)
            {
                IMarker marker = ta.markerTrack.GetMarker(0);
                director.Pause();
                director.time = marker.time;
                director.Play();
            }
        }`
  • 相关阅读:
    MySQL 安装
    MySQL简介
    图解十大经典的机器学习算法
    基于Python预测股价
    10_通过httprequest获取跟请求相关的信息
    07_参数提交时action的写法
    08_ServletConfig介绍
    05_servlet生命周期介绍
    06_url_pattern三种写法
    03_servletHello完成
  • 原文地址:https://www.cnblogs.com/terrynoya/p/13442644.html
Copyright © 2020-2023  润新知