Cysharp / R3

The new future of dotnet/reactive and UniRx.
MIT License
2.27k stars 99 forks source link

Want to flow the elements of the array one frame at a time #222

Closed kochounoyume closed 3 months ago

kochounoyume commented 5 months ago

As a goal, I want to do what the title says.

For example, the following source code does not work well in practice (because Complete is issued inside ToObservable()), but it may convey the image.

Any good ideas?


using Cysharp.Threading.Tasks;
using R3;
using UnityEngine;

public sealed class Test : MonoBehaviour
{
    void Start()
    {
        string[] array = { "a", "b", "c" };
        array
            .ToObservable()
            .SubscribeAwait(static async (element, token) =>
            {
                Debug.Log(element);
                await UniTask.Yield(token);
            }, AwaitOperation.Sequential);
    }
}
neuecc commented 3 months ago

Because the default cancelOnCompleted of SubscribeAwait has been changed, I think it is now possible to output all elements.

Showing sample is sequential, each element is output one at a time, and it is delayed by one frame with UniTask.Yield, so it does not become one frame. If you want to make all elements one frame, there are also means such as ToArray to solidify or ChunkFrame to control.