UniRx 没有 Observable.Generate() 方法

.NET Reactive Extensions 有一个整洁的方法,可以使用 corecursion 生成序列,该方法称为 Observable.Generate。


我在UniRx(Unity Reactive Extensions)中找不到该方法的等效项。你能帮我把这个代码示例移植到UniRx环境中吗?


public static IObservable<long> Timer(TimeSpan dueTime)

{

    return Observable.Generate(

        0L,

        i => i < 1,

        i => i + 1,

        i => i,

        i => dueTime

    );

}


holdtom
浏览 67回答 2
2回答

守候你守候我

Generate是UniRX中没有的运算符。您可以尝试使用操作员观察者库或使用&nbsp;Create&nbsp;运算符创建自己的生成版本。你到底想做什么?也许我可以以某种方式帮助你。

小怪兽爱吃肉

我试过了...这是工作。但我不知道可以吗?=Dpublic static IObservable<DateTime> CreateRandomTimer(int minValue, int maxValue){&nbsp; &nbsp; return Observable.CreateSafe<DateTime>(observer =>&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var random = new Random();&nbsp; &nbsp; &nbsp; &nbsp; var timer = new Timer&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Enabled = true,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Interval = random.Next(minValue, maxValue) * 1000&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; void OnTimerElapsed(object sender, ElapsedEventArgs e)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; observer.OnNext(e.SignalTime);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var currentTimer = (Timer) sender;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentTimer.Stop();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentTimer.Interval = random.Next(minValue, maxValue) * 1000;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentTimer.Start();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; timer.Elapsed += OnTimerElapsed;&nbsp; &nbsp; &nbsp; &nbsp; timer.Start();&nbsp; &nbsp; &nbsp; &nbsp; return Disposable.Create(() =>&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; timer.Elapsed -= OnTimerElapsed;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; timer.Dispose();&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; });}
打开App,查看更多内容
随时随地看视频慕课网APP