目录
Time:时间
Timer
它在⼀个给定的延迟后发射⼀个值/执行任务
Observable.Timer(TimeSpan.FromSeconds(1.0), TimeSpan.FromSeconds(0.1f))
.Subscribe(_ => Debug.Log("after 1 seconds"));
Interval
Observable.Interval(TimeSpan.FromSeconds(0.5f))
.Subscribe(times => Debug.Log(times));
Throttle
节流阀 仅在过了⼀段指定的时间还没发射数据时才发射⼀个数据
Observable.EveryUpdate()
.Where(_ => Input.GetMouseButtonDown(0))
.Throttle(TimeSpan.FromSeconds(1.0f))
.Subscribe(_ => Debug.Log("after 1 seconds"));
Delay
延迟⼀段指定的时间再发射来⾃ Observable 的发射物
Observable.Timer(TimeSpan.FromSeconds(1.0f))
.Select(_ =>
{
Debug.Log("1 seconds");
return _;
})
.Delay(TimeSpan.FromSeconds(1.0f))
.Select(_ =>
{
Debug.Log("2 seconds");
return _;
})
.Delay(TimeSpan.FromSeconds(1.0f))
.Subscribe(_ => Debug.Log("3 seconds"));
Observable.ReturnUnit()
.Delay(TimeSpan.FromSeconds(5.0f))
.Subscribe(_ => Debug.Log("has return"));
Sample
定期发射 Observable 最近发射的数据项
定时查看⼀个Observable,然后发射⾃上次采样以来它最近发射的数据
Observable.EveryUpdate()
.Where(_ => Input.GetMouseButtonDown(0))
.Select(_ => clickCount++)
.Sample(TimeSpan.FromSeconds(3))
.Subscribe(_ => Debug.LogFormat("第 {0} 次点击", clickCount));
Timestamp
给发射的数据项附加⼀个时间戳
Observable.EveryUpdate()
.Where(_ => Input.GetMouseButtonDown(0))
.Timestamp()
.Subscribe(timestamp => {
Debug.LogFormat("timestamp:{0}", timestamp.Timestamp.LocalDateTime); });
ThrottleFirst
它总是发射原始的第⼀项数据,⽽不是最近的⼀项
它默认在 computation 调度器上执⾏,但是可以使⽤第三个参数指定其它的调度器
Observable.EveryUpdate()
.Where(_ => Input.GetMouseButtonDown(0))
.ThrottleFirst(TimeSpan.FromSeconds(2.0))
.Subscribe(_ => Debug.Log("clicked!"));
⿏标点击之后,⽴即输出 “clicked”,输出之后的 2 秒内点击⽆效
TimeInterval
将⼀个发射数据的 Observable 转换为发射那些数据发射时间间隔的 Observable
Observable.EveryUpdate()
.Where(_ => Input.GetMouseButtonDown(0)).Select(_ => "clicked")
.TimeInterval()
.Subscribe(timeInterval => Debug.LogFormat("{0},{1}", timeInterval.Interval, timeInterval.Value));
表示了每次点击之间的 时间间隔
Timeout
对原始 Observable 的⼀个镜像,如果过了⼀个指定的时⻓仍没有发射数据,它会发⼀个错误通知,以⼀个 onError 通知
终⽌这个 Observable
Observable.EveryUpdate()
.Where(_ => Input.GetMouseButtonDown(0))
.Timeout(TimeSpan.FromSeconds(3.0f))
.Subscribe(_ =