facebook twitter hatena line email

Unity/UniRx/値変更検知/ReactiveProperty

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

ReactivePropertyを使った全体サンプル

using UnityEngine;
using UniRx;
public class ReactivePropertyScene : MonoBehaviour
{
    class SampleClient
    {
        public ReactiveProperty<bool> IsCompleted = new ReactiveProperty<bool>(false);
        public void Request()
        {
            IsCompleted.Value = true;
        }
    }
    void Start()
    {
        var sampleClient = new SampleClient();
        sampleClient.IsCompleted
            .Subscribe(value =>
            {
                if (!value) return;
                Debug.Log("Change IsCompleted : " + value);
            })
            .AddTo(gameObject);
        // 5秒後に遅延実行
        Observable.Timer(System.TimeSpan.FromSeconds(5))
            .Subscribe(_ => {
                sampleClient.Request();
            }).AddTo(gameObject);
    }
}