「Unity/UniRx/値変更検知/ObserveEveryValueChanged配列」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→ObserveEveryValueChangedを配列で検知できる?) |
|||
行1: | 行1: | ||
==ObserveEveryValueChangedを配列で検知できる?== | ==ObserveEveryValueChangedを配列で検知できる?== | ||
ObserveEveryValueChangedを配列の値変更をできなかった。 | ObserveEveryValueChangedを配列の値変更をできなかった。 | ||
+ | |||
+ | ==Observable.Createで配列対応したObserveEveryValueChangedを自作== | ||
+ | <pre> | ||
+ | using System.Collections; | ||
+ | using System.Collections.Generic; | ||
+ | using UnityEngine; | ||
+ | using UniRx; | ||
+ | using System; | ||
+ | |||
+ | public class ChangeValueListObservableScene : MonoBehaviour | ||
+ | { | ||
+ | class RequestData | ||
+ | { | ||
+ | public string Url; | ||
+ | public bool IsCompleted; | ||
+ | } | ||
+ | class SampleClient | ||
+ | { | ||
+ | IObservable<int> isCompletedsAsObservable; | ||
+ | public IObservable<int> IsCompletedsAsObservable { get => isCompletedsAsObservable; set => isCompletedsAsObservable = value; } | ||
+ | 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, | ||
+ | }); | ||
+ | isCompletedsAsObservable = Observable.Create<int>(observer => | ||
+ | { | ||
+ | int i = 0; | ||
+ | foreach (RequestData requestData in requestDatas) | ||
+ | { | ||
+ | int id = i; | ||
+ | requestData.ObserveEveryValueChanged(_ => _.IsCompleted) | ||
+ | .Subscribe(isCompleted => | ||
+ | { | ||
+ | Debug.Log("isCompleted=" + isCompleted); | ||
+ | observer.OnNext(id); | ||
+ | }); | ||
+ | i++; | ||
+ | } | ||
+ | return Disposable.Empty; | ||
+ | }); | ||
+ | } | ||
+ | public void Response(int id) | ||
+ | { | ||
+ | requestDatas[id].IsCompleted = true; | ||
+ | } | ||
+ | } | ||
+ | void Start() | ||
+ | { | ||
+ | int id = 1; | ||
+ | var sampleClient = new SampleClient(); | ||
+ | sampleClient.IsCompletedsAsObservable | ||
+ | .Subscribe(id => { | ||
+ | if (!sampleClient.RequestDatas[id].IsCompleted) return; | ||
+ | Debug.Log("Id=" + id + " Url=" + sampleClient.RequestDatas[id].Url + " IsCompleted=" + sampleClient.RequestDatas[id].IsCompleted); | ||
+ | }) | ||
+ | .AddTo(gameObject); | ||
+ | // 5秒後に遅延実行 | ||
+ | Observable.Timer(System.TimeSpan.FromSeconds(5)) | ||
+ | .Subscribe(_ => { | ||
+ | sampleClient.Response(id); | ||
+ | }).AddTo(gameObject); | ||
+ | } | ||
+ | } | ||
+ | </pre> | ||
==Subjectを使った配列内のキーを検知(代替)== | ==Subjectを使った配列内のキーを検知(代替)== |
2023年6月23日 (金) 16:32時点における版
目次
ObserveEveryValueChangedを配列で検知できる?
ObserveEveryValueChangedを配列の値変更をできなかった。
Observable.Createで配列対応したObserveEveryValueChangedを自作
using System.Collections; using System.Collections.Generic; using UnityEngine; using UniRx; using System; public class ChangeValueListObservableScene : MonoBehaviour { class RequestData { public string Url; public bool IsCompleted; } class SampleClient { IObservable<int> isCompletedsAsObservable; public IObservable<int> IsCompletedsAsObservable { get => isCompletedsAsObservable; set => isCompletedsAsObservable = value; } 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, }); isCompletedsAsObservable = Observable.Create<int>(observer => { int i = 0; foreach (RequestData requestData in requestDatas) { int id = i; requestData.ObserveEveryValueChanged(_ => _.IsCompleted) .Subscribe(isCompleted => { Debug.Log("isCompleted=" + isCompleted); observer.OnNext(id); }); i++; } return Disposable.Empty; }); } public void Response(int id) { requestDatas[id].IsCompleted = true; } } void Start() { int id = 1; var sampleClient = new SampleClient(); sampleClient.IsCompletedsAsObservable .Subscribe(id => { if (!sampleClient.RequestDatas[id].IsCompleted) return; Debug.Log("Id=" + id + " Url=" + sampleClient.RequestDatas[id].Url + " IsCompleted=" + sampleClient.RequestDatas[id].IsCompleted); }) .AddTo(gameObject); // 5秒後に遅延実行 Observable.Timer(System.TimeSpan.FromSeconds(5)) .Subscribe(_ => { sampleClient.Response(id); }).AddTo(gameObject); } }
Subjectを使った配列内のキーを検知(代替)
using System.Collections; using System.Collections.Generic; using UnityEngine; using UniRx; using System; public class ChangeValueListSubjectScene : MonoBehaviour { class SampleClient { public List<string> Urls = new List<string>(); Subject<int> isCompletedsAsObservable; public IObservable<int> IsCompletedsAsObservable { get => isCompletedsAsObservable; } public SampleClient() { Urls.Add("ttp://example.com"); Urls.Add("ttp://example2.com"); isCompletedsAsObservable = new Subject<int>(); } public void Response(int id) { isCompletedsAsObservable.OnNext(id); } } void Start() { int id = 1; var sampleClient = new SampleClient(); sampleClient.IsCompletedsAsObservable .Subscribe(id => { Debug.Log("Id=" + id + " sampleClient.Url=" + sampleClient.Urls[id]); }) .AddTo(gameObject); // 5秒後に遅延実行 Observable.Timer(System.TimeSpan.FromSeconds(5)) .Subscribe(_ => { sampleClient.Response(id); }).AddTo(gameObject); } }
EveryUpdateを使った配列内のデータを検知(代替)
毎フレームごとに処理されるので危険かも。
public class ChangeValueListScene : MonoBehaviour { class SampleClient { public List<string> Urls = new List<string>(); public List<bool> IsCompleteds = new List<bool>(); public SampleClient() { Urls.Add("ttp://example.com"); Urls.Add("ttp://example2.com"); IsCompleteds.Add(false); IsCompleteds.Add(false); } public void Response(int id) { IsCompleteds[id] = true; } } void Start() { int id = 0; var sampleClient = new SampleClient(); // 毎フレームごとに処理されるので危険かも。 var observable = Observable.EveryUpdate() .Subscribe(_ => { Debug.Log("IsCompleteds.Count=" + sampleClient.IsCompleteds.Count); int cnt = sampleClient.IsCompleteds.Count; for (int tmpId = 0; tmpId < cnt; tmpId++) { if (!sampleClient.IsCompleteds[tmpId]) continue; Debug.Log("Id=" + tmpId + " sampleClient.Url=" + sampleClient.Urls[tmpId]); sampleClient.IsCompleteds[tmpId] = false; // そのままにすると、連続してここが処理されるので、初期化 } }) .AddTo(gameObject); // 5秒後に遅延実行 Observable.Timer(System.TimeSpan.FromSeconds(5)) .Subscribe(_ => { sampleClient.Response(id); }).AddTo(gameObject); } }