1

.NET Reactive Extensions has a neat method to generate sequences using corecursion which is called Observable.Generate.

I can't find an equivalent for that method in UniRx (Unity Reactive Extensions). Could you please help me to port this code sample into UniRx environment?

public static IObservable<long> Timer(TimeSpan dueTime)
{
    return Observable.Generate(
        0L,
        i => i < 1,
        i => i + 1,
        i => i,
        i => dueTime
    );
}
4
  • This should work just as-is. Feb 2, 2019 at 7:43
  • 1
    Sadly no. Generate function is not found in the API. Feb 2, 2019 at 7:50
  • Sadly, yes, it must be. It is core to Rx. If you have Observable you must have it. Feb 2, 2019 at 8:03
  • I am following Mr. Lee Campbell's book on Rx.NET. Observable class is available and creating, transitioning, transforming work, except the Observable.Generate() method. I'll perform a string search for the Generate method in the UniRx repo today. Feb 2, 2019 at 8:08

3 Answers 3

1

Generate is an operator that's not featured into UniRX.

You can try to create your own version of the Generate by using Operator Observer Base or use the Create operator.

What are you trying to do exactly? Maybe I can help you in some way.

0

I tried and ... It is work. But I don't know is it OK? =D

public static IObservable<DateTime> CreateRandomTimer(int minValue, int maxValue)
{
    return Observable.CreateSafe<DateTime>(observer =>
    {
        var random = new Random();

        var timer = new Timer
        {
            Enabled = true,
            Interval = random.Next(minValue, maxValue) * 1000
        };
        
        void OnTimerElapsed(object sender, ElapsedEventArgs e)
        {
            observer.OnNext(e.SignalTime);

            var currentTimer = (Timer) sender;
            currentTimer.Stop();
            currentTimer.Interval = random.Next(minValue, maxValue) * 1000;
            currentTimer.Start();
        }

        timer.Elapsed += OnTimerElapsed;
        timer.Start();

        return Disposable.Create(() =>
        {
            timer.Elapsed -= OnTimerElapsed;
            timer.Dispose();
        });
    });
}
0

I understand that question is outdated probably. But still I'll answer.

If you wish to use timer with random time, then you should start post from this question. Because it costs extra time to understand from code sample what do you actually want.

And here is the answer:

using System;
using UniRx;
using UnityEngine;

public class TimerSample : MonoBehaviour
{
    [SerializeField] int minValue;
    [SerializeField] int maxValue;

    void Start()
    {
        int seconds = UnityEngine.Random.RandomRange(minValue, maxValue);
        TimeSpan period = TimeSpan.FromSeconds(seconds);
        Observable.Timer(period, Scheduler.MainThreadIgnoreTimeScale)
            .Subscribe(DoSomething)
            .AddTo(this);
    }

    private void DoSomething(long obj)
    {
        throw new NotImplementedException();
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.