目录
NextFrame
下一帧
void Start()
{
Debug.Log(Time.frameCount);
StartCoroutine(NextFrame(() => Debug.Log(Time.frameCount)));
Observable.NextFrame()
.Subscribe(_ => Debug.LogFormat("Observable NextFrame:{0}", Time.frameCount));
}
IEnumerator NextFrame(Action callback)
{
yield return new WaitForEndOfFrame();
callback();
}
DelayFrame
延迟帧数
Debug.Log(Time.frameCount);
Observable.ReturnUnit()
.Do(_ => Debug.Log(Time.frameCount))
.DelayFrame(10)
.Do(_ => Debug.Log(Time.frameCount))
.Subscribe(_ => Debug.Log(Time.frameCount));
FrameInterval
计算/累计/计时 帧数
Observable.EveryUpdate()
.Where(_ => Input.GetMouseButtonDown(0))
.Timestamp()
.TimeInterval()
.FrameInterval()
.Subscribe(frameInterval => Debug.LogFormat("距离上一次点击的帧数:{0},时间间隔:{1}", frameInterval.Interval,
frameInterval.Value.Interval));
BatchFrame
收集 100 帧内鼠标的点击事件然后输出
Observable.EveryUpdate()
.Where(_ => Input.GetMouseButtonDown(0))
.BatchFrame(100, FrameCountType.EndOfFrame)
.Subscribe(clicks =>
{
Debug.Log(clicks.Count);
});
ForEachAsync
Observable.Range(0, 200) // 0 1 2
.ForEachAsync(number => Debug.LogFormat("for each:{0}", number))
.Subscribe(number => Debug.LogFormat("subsrcibe:{0}", number));
FrameTimeInterval
当点击⿏标时,返回距离上⼀次点击的,帧总时间
Observable.EveryUpdate()
.Where(_ => Input.GetMouseButtonDown(0))
.FrameTimeInterval()
.Subscribe(timeInterval => Debug.LogFormat("observable 1:{0}", timeInterval.Interval));
Observable.EveryUpdate()
.Where(_ => Input.GetMouseButtonUp(0))
.TimeInterval()
.Subscribe(timeInterval => Debug.LogFormat("observable 2:{0}", timeInterval.Interval));
SampleFrame
间隔指定帧数 执行方法
Observable.EveryUpdate()
.SampleFrame(5)
.Subscribe(frameCount => { Debug.Log(Time.frameCount); });
RepeatUntilDestroy
每隔⼀秒输出⼀次 ticked,当把脚本所在的 GameObject 删除掉,则停止输出
Observable.Timer(TimeSpan.FromSeconds(1.0f))
.RepeatUntilDestroy(this)
.Subscribe(_ => Debug.Log("ticked"));
RepeatUntilDisable
每隔⼀秒输出一次 ticked,当把脚本所在的 GameObject 隐藏掉,则停⽌输出
Observable.Timer(TimeSpan.FromSeconds(1.0f))
.RepeatUntilDisable(this)
.Subscribe(_ => Debug.Log("ticked"), () => Debug.Log("completed"));
ObserveOnMainThread
Debug.Log(Time.time);
Observable.Start(() =>
{
Thread.Sleep(TimeSpan.FromSeconds(1.0f));
return "hello thread";
}).ObserveOnMainThread()
.Subscribe(threadResult =>
{
Debug.LogFormat("{0} {1}", threadResult, Time.time);
});
DelayFrameSubscription
延迟一段时间后 开始接收数据
Debug.Log(Time.frameCount);
Observable.ReturnUnit()
.DelayFrameSubscription(10)
.Subscribe(_ => Debug.Log(Time.frameCount));
ThrottleFirstFrame
每 100 帧内的第⼀次点击事件输出 clicked
Observable.EveryUpdate()
.Where(_ => Input.GetMouseButtonDown(0))
.ThrottleFirstFrame(100)
.Subscribe(_ => Debug.Log("clicked"));
Observable.EveryUpdate()
.SampleFrame(100)
.Subscribe(_ => Debug.Log("frame ended"));
ThrottleFrame
⿏标点击的 100 帧内,若没有⿏标点击事件,则在这个 100 帧后输出,否则重新开始计算帧数
Observable.EveryUpdate()
.Where(_ => Input.GetMouseButtonDown(0)).Select(_ => ++i)
.Do(id => Debug.LogFormat("clicked realtime:{0}", id))
.ThrottleFrame(100)
.Do(_ => Debug.Log("end 100 frame"))
.Subscribe(id => Debug.LogFormat("processd {0} clicked", id));
TimeoutFrame
超过 120 帧不进⾏⿏标点击时,输出
Observable.EveryUpdate()
.Where(_ => Input.GetMouseButtonDown(0))
.Do(_ => Debug.LogFormat("clicked frame:{0}", Time.frameCount))
.TimeoutFrame(120)
.Subscribe(_ => Debug.Log("mouse clicked"), e =>
{
Debug.LogFormat("excpetion frame count:{0}", Time.frameCount);
});
TakeUntilDestroy
每次按下⿏标左键,输出 mouse clicked,将该脚本所在的 GameObject 销毁后,点击⿏标不再输出
Observable.EveryUpdate()
.Where(_ => Input.GetMouseButtonDown(0))
.TakeUntilDestroy(this)
.Subscribe(_ => Debug.Log("mouse clicked"));
TakeUntilDisable
每次按下⿏标左键,输出 mouse clicked,将该脚本所在的 GameObject 隐藏掉后,点击⿏标不再输出
Observable.EveryUpdate()
.Where(_ => Input.GetMouseButtonDown(0))
.Do(_=>Debug.Log("do mouse button downed"))
.TakeUntilDisable(this)
.Subscribe(_ => Debug.Log("mouse clicked"));