facebook twitter hatena line email

Unity/UniRx/値変更検知/配列数

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UniRx;
using System;

public class ChangeValueListCountScene : MonoBehaviour
{
    class RequestData
    {
        public string Url;
        public bool IsCompleted;
    }
    class SampleClient
    {
        List<RequestData> requestDatas;
        public List<RequestData> RequestDatas { get => requestDatas; set => requestDatas = value; }
        public SampleClient()
        {
            requestDatas = new List<RequestData>();
            requestDatas.Add(new RequestData
            {
                Url = "ttp://example.com",
                IsCompleted = false,
            });
            requestDatas.Add(new RequestData
            {
                Url = "ttp://example2.com",
                IsCompleted = false,
            });
        }
        public void Add()
        {
            requestDatas.Add(new RequestData
            {
                Url = "ttp://example3.com",
                IsCompleted = false,
            });
        }
    }
    void Start()
    {
        var sampleClient = new SampleClient();
        sampleClient.RequestDatas.ObserveEveryValueChanged(_ => _.Count)
            .Subscribe(count =>
            {
                Debug.Log("count=" + count);
            }).AddTo(gameObject);

        // 5秒後に遅延実行
        Observable.Timer(System.TimeSpan.FromSeconds(5))
            .Subscribe(_ => {
                sampleClient.Add();
            }).AddTo(gameObject);
    }
}