先来看下效果,播放的结尾后回到前面的某一个位置,然后循环
具体实现思路如下:
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();
}
}`