UniRx操作符_UniRx独有

本文详细介绍了UniRx库中与帧同步相关的操作符,如NextFrame、DelayFrame、ThrottleFrame等,它们在Unity开发中用于实现特定帧的事件处理、延迟执行和频率限制等功能,帮助优化游戏逻辑并提高性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值