「Unity/R3/値変更検知/ReactivePropertyData」の版間の差分
提供: 初心者エンジニアの簡易メモ
行6: | 行6: | ||
public class ReactivePropertyDataScene : MonoBehaviour | public class ReactivePropertyDataScene : MonoBehaviour | ||
{ | { | ||
+ | SampleReactiveProperty sampleReactiveProperty; | ||
class SampleData | class SampleData | ||
{ | { | ||
行12: | 行13: | ||
class SampleReactiveProperty | class SampleReactiveProperty | ||
{ | { | ||
+ | private CompositeDisposable disposables = new CompositeDisposable(); | ||
private SampleData sampleData; | private SampleData sampleData; | ||
public ReactiveProperty<bool> IsCompleted; | public ReactiveProperty<bool> IsCompleted; | ||
行20: | 行22: | ||
// ReactiveProperty の変更を監視し、SampleData に反映 | // ReactiveProperty の変更を監視し、SampleData に反映 | ||
− | IsCompleted.Subscribe(value => sampleData.IsCompleted = value); | + | IsCompleted.Subscribe(value => sampleData.IsCompleted = value) |
+ | .AddTo(disposables); | ||
} | } | ||
public void Request() | public void Request() | ||
{ | { | ||
IsCompleted.Value = true; | IsCompleted.Value = true; | ||
+ | } | ||
+ | public void Dispose() | ||
+ | { | ||
+ | disposables?.Dispose(); | ||
} | } | ||
} | } | ||
行30: | 行37: | ||
{ | { | ||
var sampleData = new SampleData(); | var sampleData = new SampleData(); | ||
− | + | sampleReactiveProperty = new SampleReactiveProperty(sampleData); | |
sampleReactiveProperty.IsCompleted | sampleReactiveProperty.IsCompleted | ||
.Subscribe(value => | .Subscribe(value => | ||
行45: | 行52: | ||
sampleReactiveProperty.Request(); | sampleReactiveProperty.Request(); | ||
}).AddTo(gameObject); | }).AddTo(gameObject); | ||
+ | } | ||
+ | void OnDestroy() | ||
+ | { | ||
+ | sampleReactiveProperty?.Dispose(); | ||
} | } | ||
} | } | ||
</pre> | </pre> |
2025年1月30日 (木) 11:07時点における最新版
Dataクラスを維持したまま、ReactivePropertyを使う全体サンプル
using UnityEngine; using R3; public class ReactivePropertyDataScene : MonoBehaviour { SampleReactiveProperty sampleReactiveProperty; class SampleData { public bool IsCompleted = false; } class SampleReactiveProperty { private CompositeDisposable disposables = new CompositeDisposable(); private SampleData sampleData; public ReactiveProperty<bool> IsCompleted; public SampleReactiveProperty(SampleData sampleData) { this.sampleData = sampleData; IsCompleted = new ReactiveProperty<bool>(sampleData.IsCompleted); // ReactiveProperty の変更を監視し、SampleData に反映 IsCompleted.Subscribe(value => sampleData.IsCompleted = value) .AddTo(disposables); } public void Request() { IsCompleted.Value = true; } public void Dispose() { disposables?.Dispose(); } } void Start() { var sampleData = new SampleData(); sampleReactiveProperty = new SampleReactiveProperty(sampleData); sampleReactiveProperty.IsCompleted .Subscribe(value => { if (!value) return; Debug.Log("Change IsCompleted : " + value); Debug.Log("Change sampleData.IsCompleted : " + sampleData.IsCompleted); }) .AddTo(gameObject); // 5秒後に遅延実行 Observable.Timer(System.TimeSpan.FromSeconds(5)) .Subscribe(_ => { sampleReactiveProperty.Request(); }).AddTo(gameObject); } void OnDestroy() { sampleReactiveProperty?.Dispose(); } }