Delay
DelayFrame
DelayFrameSubscription
DelaySubscription
Delay
延迟一定的时间来继续执行之前的流,延迟的时间不计算在基础流里面,权当附加的一个功能,只针对基于时间的流有效果,图解
输出结果
代码示例
public class No05_Delay : MonoBehaviour
{
void Start()
{
var timerStream = Observable.Timer(TimeSpan.FromSeconds(3f));
var timerSubscribe = timerStream.Subscribe(_ => Debug.Log("after 3 seconds"));
var delayStream = timerStream.Delay(TimeSpan.FromSeconds(1f));
var disposable = delayStream.Subscribe(Next, Error, Complete);
disposable.AddTo(gameObject);
timerSubscribe.AddTo(gameObject);
}
void Next(long time)
{
Debug.LogFormat("delay 1 second");
}
void Error(Exception ex)
{
Debug.LogException(ex);
}
void Complete()
{
Debug.LogFormat("Complete");
}
}
代码解析
代码中订阅了2次,每一个流都是独立执行的,可以进行单独的订阅;只要流没有执行Disposable,则流执行的内容一直保留,Delay只针对基于时间TimeSpan的基础流有效果
DelayFrame
延迟一定的帧数继续执行之前的流,延迟帧数不计算在基础流里面,图解
执行结果
代码示例
public class No05_Delay : MonoBehaviour
{
void Start()
{
CompositeDisposable disposables = new CompositeDisposable();
var updateStream = Observable.EveryUpdate();
var mouseDownStream = updateStream.Where(_ => Input.GetMouseButtonDown((int) MouseButton.LeftMouse));
var disposable = mouseDownStream.Subscribe(
frame => Debug.LogFormat("点击事件发生在{0}帧", frame),
ex => Debug.LogException(ex),
() => Debug.LogFormat("Complete"));
var delayFrameStream = mouseDownStream.DelayFrame(60);
var disposable1 = delayFrameStream.Subscribe(Next, Error, Complete);
disposables.Add(disposable);
disposables.Add(disposable1);
this.OnDestroyAsObservable().Subscribe(_ => disposables.Dispose());
}
void Next(long frame)
{
Debug.LogFormat("延迟事件发生在{0}帧", frame + 60);
}
void Error(Exception ex)
{
Debug.LogException(ex);
}
void Complete()
{
Debug.LogFormat("Complete");
}
}
代码解析
CompositeDisposable是内置提供的一个disposable集合工具类,在GameObject销毁的时候调用集合的Dispose()来解除所有的数据方法绑定;使用this.OnDestroyAsObservable()可以将Destroy方法使用UniRx来管理,在Destroy的时候去释放内存;Delay的功能有点类似于Pause,把基础流给暂停了,延迟结束以后在继续执行基础流的数据。
DelayFrameSubscription
延迟一定帧数给订阅者订阅,图解,同上
输出结果
代码示例
public class No05_Delay : MonoBehaviour
{
void Start()
{
CompositeDisposable disposables = new CompositeDisposable();
var updateStream = Observable.EveryUpdate();
var mouseDownStream = updateStream.Where(_ => Input.GetMouseButtonDown((int) MouseButton.LeftMouse));
var disposable = mouseDownStream.Subscribe(
frame => Debug.LogFormat("点击事件发生在{0}帧", frame),
ex => Debug.LogException(ex),
() => Debug.LogFormat("Complete"));
var delayFrameSubscriptionStream = mouseDownStream.DelayFrameSubscription(100);
var disposable1 = delayFrameSubscriptionStream.Subscribe(Next, Error, Complete);
disposables.Add(disposable);
disposables.Add(disposable1);
this.OnDestroyAsObservable().Subscribe(_ => disposables.Dispose());
}
void Next(long frame)
{
Debug.LogFormat("延迟订阅事件发生在{0}帧", frame);
}
void Error(Exception ex)
{
Debug.LogException(ex);
}
void Complete()
{
Debug.LogFormat("Complete");
}
}
代码解析
DelaySubscription
同DelayFrameSubscription,但这个是基于时间来Delay处理
总结
基于时间单位在UniRx中表现分为两类,时间和帧,时间处理的基础流需要使用时间的功能流来进行斜街;基于帧处理的基础流需要使用帧的功能流来衔接。
UniRx中的执行的一个挨个一个的,但为什么上一个流没有执行Complete方法呢,Rx内部机制决定的,当一个Observable执行OnNext方法的时候,会判定下一个Next是否为空,不空则执行;所以中途订阅的流不会执行Complete,只是一个执行结果的输出;
如果你觉得对你有那么一点帮助,请点赞打赏是给我最大的动力
呀哈哈,有能力的朋友可以扫下面的打赏二维码↓↓↓
谢谢看官们↓↓↓