Skip to content

Why when using ThrottleLast can't change text correctly? #359

@heroliss

Description

@heroliss
using UnityEngine;
using UnityEngine.UIElements;
using R3;
using TMPro;
using System;

public class UITest : MonoBehaviour
{
    [SerializeField] UIDocument uiDocument;
    VisualElement uiRoot;

    [SerializeField] TextMeshProUGUI label;

    ReactiveProperty<int> clickCount = new(0);

    private void Start()
    {
        uiRoot = uiDocument.rootVisualElement;
        var buttons = uiRoot.Query<Button>().ToList();
        foreach (var button in buttons)
        {
            var clickStream = Observable.FromEvent((action) => button.clicked += action, (action) => button.clicked -= action, destroyCancellationToken);
            clickStream.Subscribe(_ =>
            {
                clickCount.Value++;
            });
        }

        // Issue occurs here:

        clickCount.ThrottleLast(TimeSpan.FromSeconds(1))  //---> When using ThrottleFirst, text changes are displayed correctly
            //.ObserveOnCurrentSynchronizationContext() //---> When this method is added, text changes are displayed correctly
            .Subscribe(x => {
            label.text = $"Click Count: {x}"; //===> When using ThrottleLast,  label.text changes are visible in the inspector, but not updated in the Game View. Why?
        });

        label.text = "111"; // this is correctly
    }
}

unity 6000.2.10f1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions