「Unity/R3/UniRxから移植」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→ObserveEveryValueChangedはEveryValueChangedへ) |
(→ObservableEventTriggerを設置してる場合は) |
||
(同じ利用者による、間の30版が非表示) | |||
行1: | 行1: | ||
+ | ==公式== | ||
+ | 変更メソッド名一覧 | ||
+ | |||
+ | https://github.com/Cysharp/R3?tab=readme-ov-file#classmethod-name-changes-from-dotnetreactive-and-neueccunirx | ||
+ | |||
==using置換== | ==using置換== | ||
<pre> | <pre> | ||
行4: | 行9: | ||
+ using R3; | + using R3; | ||
- using UniRx.Triggers; | - using UniRx.Triggers; | ||
− | + using R3.Triggers; | + | + using R3.Triggers; |
+ | - using System; | ||
</pre> | </pre> | ||
行12: | 行18: | ||
<pre> | <pre> | ||
// UniRx | // UniRx | ||
− | transform.ObserveEveryValueChanged(_ => _.Count).Subscribe(count => Debug.Log(count)); | + | transform.ObserveEveryValueChanged(_ => _.Count).Subscribe(count => Debug.Log(count)) |
+ | .AddTo(gameObject); | ||
// R3 | // R3 | ||
+ | CompositeDisposable disposable; | ||
+ | disposable = new CompositeDisposable(); | ||
Observable.EveryValueChanged(transform, _ => _.Count) | Observable.EveryValueChanged(transform, _ => _.Count) | ||
− | + | .Subscribe(count => Debug.Log(count)) | |
+ | .AddTo(disposable); | ||
</pre> | </pre> | ||
参考:https://qiita.com/toRisouP/items/4344fbcba7b7e8d8ce16 | 参考:https://qiita.com/toRisouP/items/4344fbcba7b7e8d8ce16 | ||
+ | |||
+ | ==IObservableをObservableへ== | ||
+ | ===Collisionの置換の場合=== | ||
+ | <pre> | ||
+ | // UniRx | ||
+ | ObservableCollision2DTrigger landTrigger; | ||
+ | IObservable<Collision2D> landOnCollisionEnterAsObservable = landTrigger.OnCollisionEnter2DAsObservable(); | ||
+ | |||
+ | // R3 | ||
+ | ObservableCollision2DTrigger landTrigger; | ||
+ | Observable<Collision2D> landOnCollisionEnterAsObservable = landTrigger.OnCollisionEnter2DAsObservable(); | ||
+ | </pre> | ||
+ | |||
+ | ===Observable.Createの置換の場合=== | ||
+ | <pre> | ||
+ | // UniRx | ||
+ | IObservable<int> isCompletedsAsObservable = Observable.Create<int>(observer => | ||
+ | { | ||
+ | return Disposable.Empty; | ||
+ | }); | ||
+ | // R3 | ||
+ | Observable<int> isCompletedsAsObservable = Observable.Create<int>(observer => | ||
+ | { | ||
+ | return Disposable.Empty; | ||
+ | }); | ||
+ | </pre> | ||
+ | |||
+ | ===ObservableEventTriggerを設置してる場合は=== | ||
+ | InspectorにUnityEngineBridge/Triggers/ObservableEventTriggerを設置してる場合は、 | ||
+ | R3/Runtime/Triggers/ObservableEventTriggerへ切り替える。 | ||
+ | |||
+ | usingも、UniRx.Triggersから、R3.Triggersへ | ||
+ | <pre> | ||
+ | - using UniRx.Triggers; | ||
+ | + using R3.Triggers; | ||
+ | </pre> | ||
+ | |||
+ | ==Observable.EveryUpdate()~AddTo()の返り値まわり対応== | ||
+ | <pre> | ||
+ | //UniRx | ||
+ | var observable = Observable.EveryUpdate() | ||
+ | .Subscribe(_ => { | ||
+ | }).AddTo(disposable); | ||
+ | |||
+ | // R3 | ||
+ | Observable.EveryUpdate() | ||
+ | .Subscribe(_ => { | ||
+ | // 略 | ||
+ | }).AddTo(disposable); | ||
+ | </pre> | ||
+ | |||
+ | ==ReactiveCollectionをObservableList<T>へ== | ||
+ | ObservableList では、`using ObservableCollections;`を追加する。 | ||
+ | |||
+ | <pre> | ||
+ | // UniRx | ||
+ | private ReactiveCollection<string> users = new ReactiveCollection<string>(); | ||
+ | users | ||
+ | .ObserveAdd() | ||
+ | .Subscribe(value => | ||
+ | { | ||
+ | Debug.Log($"[Add]Index={value.Index},Value={value.Value}"); | ||
+ | }); | ||
+ | |||
+ | // R3 | ||
+ | using ObservableCollections; | ||
+ | private ObservableList<string> users = new ObservableList<string>(); | ||
+ | users | ||
+ | .ObserveAdd() | ||
+ | .Subscribe(value => | ||
+ | { | ||
+ | Debug.Log($"[Add]Index={value.Index},Value={value.Value}"); | ||
+ | }); | ||
+ | |||
+ | </pre> | ||
+ | |||
+ | ==SubjectのSubscribeの置換== | ||
+ | <pre> | ||
+ | // UniRx | ||
+ | Subject<string> subject = new Subject<string>(); | ||
+ | subject | ||
+ | .Subscribe( | ||
+ | message => Debug.Log("msg1:" + message), | ||
+ | error => Debug.LogError("Error" + error) | ||
+ | () => Debug.Log("Completed") | ||
+ | ); | ||
+ | |||
+ | // R3 | ||
+ | Subject<string> subject = new Subject<string>(); | ||
+ | subject | ||
+ | .Subscribe( | ||
+ | message => Debug.Log("msg1:" + message), | ||
+ | error => Debug.LogError("Error" + error) | ||
+ | ); | ||
+ | </pre> | ||
+ | ==TakeUntilDestroy()はAddTo()へ置換== | ||
+ | TakeUntilDestroyは、R3に存在しないので、AddTo()へ | ||
+ | <pre> | ||
+ | // UniRx | ||
+ | button.OnClickAsObservable() | ||
+ | .TakeUntilDestroy(gameObject) | ||
+ | .Subscribe(_ => { | ||
+ | Debug.Log("click!"); | ||
+ | }); | ||
+ | |||
+ | // R3 | ||
+ | CompositeDisposable disposable; | ||
+ | disposable = new CompositeDisposable(); | ||
+ | button.OnClickAsObservable() | ||
+ | .Subscribe(_ => { | ||
+ | Debug.Log("click!"); | ||
+ | }) | ||
+ | .AddTo(disposable); | ||
+ | </pre> | ||
+ | 参考:https://naninunenoy.hatenablog.com/entry/2024/03/07/001818 |
2024年6月24日 (月) 20:13時点における最新版
目次
公式
変更メソッド名一覧
using置換
- using UniRx; + using R3; - using UniRx.Triggers; + using R3.Triggers; - using System;
参考:https://zenn.dev/tkada/articles/a96258e080056a
ObserveEveryValueChangedはEveryValueChangedへ
// UniRx transform.ObserveEveryValueChanged(_ => _.Count).Subscribe(count => Debug.Log(count)) .AddTo(gameObject); // R3 CompositeDisposable disposable; disposable = new CompositeDisposable(); Observable.EveryValueChanged(transform, _ => _.Count) .Subscribe(count => Debug.Log(count)) .AddTo(disposable);
参考:https://qiita.com/toRisouP/items/4344fbcba7b7e8d8ce16
IObservableをObservableへ
Collisionの置換の場合
// UniRx ObservableCollision2DTrigger landTrigger; IObservable<Collision2D> landOnCollisionEnterAsObservable = landTrigger.OnCollisionEnter2DAsObservable(); // R3 ObservableCollision2DTrigger landTrigger; Observable<Collision2D> landOnCollisionEnterAsObservable = landTrigger.OnCollisionEnter2DAsObservable();
Observable.Createの置換の場合
// UniRx IObservable<int> isCompletedsAsObservable = Observable.Create<int>(observer => { return Disposable.Empty; }); // R3 Observable<int> isCompletedsAsObservable = Observable.Create<int>(observer => { return Disposable.Empty; });
ObservableEventTriggerを設置してる場合は
InspectorにUnityEngineBridge/Triggers/ObservableEventTriggerを設置してる場合は、 R3/Runtime/Triggers/ObservableEventTriggerへ切り替える。
usingも、UniRx.Triggersから、R3.Triggersへ
- using UniRx.Triggers; + using R3.Triggers;
Observable.EveryUpdate()~AddTo()の返り値まわり対応
//UniRx var observable = Observable.EveryUpdate() .Subscribe(_ => { }).AddTo(disposable); // R3 Observable.EveryUpdate() .Subscribe(_ => { // 略 }).AddTo(disposable);
ReactiveCollectionをObservableList<T>へ
ObservableList では、`using ObservableCollections;`を追加する。
// UniRx private ReactiveCollection<string> users = new ReactiveCollection<string>(); users .ObserveAdd() .Subscribe(value => { Debug.Log($"[Add]Index={value.Index},Value={value.Value}"); }); // R3 using ObservableCollections; private ObservableList<string> users = new ObservableList<string>(); users .ObserveAdd() .Subscribe(value => { Debug.Log($"[Add]Index={value.Index},Value={value.Value}"); });
SubjectのSubscribeの置換
// UniRx Subject<string> subject = new Subject<string>(); subject .Subscribe( message => Debug.Log("msg1:" + message), error => Debug.LogError("Error" + error) () => Debug.Log("Completed") ); // R3 Subject<string> subject = new Subject<string>(); subject .Subscribe( message => Debug.Log("msg1:" + message), error => Debug.LogError("Error" + error) );
TakeUntilDestroy()はAddTo()へ置換
TakeUntilDestroyは、R3に存在しないので、AddTo()へ
// UniRx button.OnClickAsObservable() .TakeUntilDestroy(gameObject) .Subscribe(_ => { Debug.Log("click!"); }); // R3 CompositeDisposable disposable; disposable = new CompositeDisposable(); button.OnClickAsObservable() .Subscribe(_ => { Debug.Log("click!"); }) .AddTo(disposable);
参考:https://naninunenoy.hatenablog.com/entry/2024/03/07/001818