目錄
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"));