UniRx操作符补充
1.NextFrame
在下一帧执行
public class NextFrameSample : MonoBehaviour
{
private void Start()
{
Application.targetFrameRate = 1;
Observable.NextFrame(FrameCountType.EndOfFrame)
.Subscribe(_ =>
{
Debug.Log("Start:"+Time.frameCount);
});
}
private void Update()
{
Debug.Log( Time.frameCount);
if (Input.GetKeyDown(KeyCode.Space))
{
//下一帧的Updata执行
Observable.NextFrame()
.Subscribe(_ =>
{
Debug.Log("Update"+Time.frameCount);
});
//下一帧的FixedUpdate执行
Observable.NextFrame(FrameCountType.FixedUpdate)
.Subscribe(_ =>
{
Debug.Log("FixedUpdate"+Time.frameCount);
});
//下一帧结束执行
Observable.NextFrame(FrameCountType.EndOfFrame)
.Subscribe(_ =>
{
Debug.Log("EndOfFrame"+Time.frameCount);
});
}
}
}
2.DelayFrame
延帧执行
Observable.ReturnUnit()
.Do(_ => Debug.Log(Time.frameCount))
.DelayFrame(10)
.Subscribe(_ => Debug.Log(Time.frameCount));
Observable.Return(Unit.Default)
.DelayFrame(1)
.Subscribe(_ => Debug.Log("Next Update"));
Observable.Return(Unit.Default)
.DelayFrame(1, FrameCountType.FixedUpdate)
.Subscribe(_ => Debug.Log("Next FixedUpdate"));
3.FrameInterval
帧数间隔
Observable.EveryUpdate()
.Where(_ => Input.GetMouseButtonDown(0))
.FrameInterval()
.Subscribe(frameInterval => Debug.Log(frameInterval.Interval));
//输出距离上⼀次⿏标点击所间隔的帧数
4.BatchFrame
将指定帧数期间发出的 OnNext 消息合并为一条
Observable.EveryUpdate()
.Where(_ => Input.GetMouseButtonDown(0))
.BatchFrame(100, FrameCountType.EndOfFrame)
.Subscribe(clicks =>
{
Debug.Log(clicks.Count);
});
//收集 每 100 帧内的点击事件,然后进⾏统⼀的输出。
//使用BatchFrame将时间从Update转换为FixedUpdate的代码。
this.UpdateAsObservable()
.Where(_ => Input.GetKeyDown(KeyCode.Space))
.AsUnitObservable() // 转换为Unit型
.BatchFrame(0, FrameCountType.FixedUpdate)
.Subscribe(_ =>
{
rigidBody.AddForce(Vector3.up * 10, ForceMode.VelocityChange);
});
5.ForEachAsync
ForEachAsync通过异步的方式将一个数据列表全部遍历出来
Observable.Range(0, 10)
.ForEachAsync(number => Debug.Log(number))
.Subscribe();
6.FrameTimeInterval
与TimeInterval相同,只是时间的测量方式不同
Observable.EveryUpdate()
.Where(_ => Input.GetMouseButtonDown(0))
.FrameTimeInterval()
.Subscribe(frameTimeInterval =>
Debug.Log(frameTimeInterval.Interval));
7.SampleFrame
以指定的帧间隔对 OnNext 消息进行采样
Observable.EveryUpdate()
.SampleFrame(5)
.Subscribe(_ => Debug.Log(Time.frameCount));
this.UpdateAsObservable ().Where (_ => Input.GetMouseButtonDown (0))
.Select (x => 1)
.Scan ((a, b) => a + b)
.SampleFrame (30)
.Subscribe (x => Debug.Log ("累计" + x + "click!!!"));
8.RepeatUntilDestroy
一直重复执行基础流和功能流,直到当前的GameObject销毁之后才停止
Observable.Timer(TimeSpan.FromSeconds(1.0f))
.RepeatUntilDestroy(this)
.Subscribe(_ => Debug.Log("ticked"));
9.ObserveOnMainThread
将一个流转移到主线程进行观察
Observable.Start(() =>
{
Thread.Sleep(TimeSpan.FromSeconds(1.0f));
return 1;
}).ObserveOnMainThread()
.Subscribe(threadResult => Debug.LogFormat("{0} {1}",
threadResult, Time.time));
10.DelayFrameSubscript
延迟订阅,在运行 Subscribe 之前等待指定的帧数
Observable.ReturnUnit()
.DelayFrameSubscription(1000)
.Subscribe(_ => Debug.Log(Time.time));
11.ThrottleFirstFrame
OnNext 后在一段时间内忽略该消息
Observable.EveryUpdate()
.Where(_ => Input.GetMouseButtonDown(0))
.ThrottleFirstFrame(30)
.Subscribe(_ => Debug.Log("clicked"));
//每 30 帧内的第⼀次点击事件输出 clicked
12.ThrottleFrame
节流帧数,屏蔽后续输入,并延迟输出。如果在短时间内输入了大量消息,在指定的时间只取出最后一条数据。
Observable.EveryUpdate()
.Where(_ => Input.GetMouseButtonDown(0))
.ThrottleFrame(100)
.Subscribe(_ => Debug.Log("clicked"));
//⿏标点击的 100 帧内,没有⿏标点击事件,则在 100 帧后输出,否则重新计算帧数
13.TimeoutFrame
如果过了指定帧数仍没有发射数据,会发送错误通知
Observable.EveryUpdate()
.Where(_ => Input.GetMouseButtonDown(0))
.TimeoutFrame(100)
.Subscribe(_ => Debug.Log("error"));
14.TakeUntilDestroy
当指定物体被销毁时,丢弃原始Observable发射的任何数据
Observable.EveryUpdate()
.Where(_ => Input.GetMouseButtonDown(0))
.TakeUntilDestroy(this)
.Subscribe(_ => Debug.Log("mouse clicked"));
15.TakeUntilDisable
当指定物体被禁用时,丢弃原始Observable发射的任何数据
16.RepeatUntilDisable
重复执行,直到指定物体被禁止
Observable.Timer(TimeSpan.FromSeconds(1.0f))
.RepeatUntilDisable(this)
.Subscribe(_ => Debug.Log("RepeatUntilDisable"));
17.AsUnitObservable
将OnNext消息的类型转换为类型Unit
其实就是Select(_ => Unit.Default) 的缩写
Observable.EveryUpdate()
.AsUnitObservable().Subscribe(x =>
{
//x类型从long变为了unit
});

浙公网安备 33010602011771号