facebook twitter hatena line email

「Unity/R3/UniRxから移植」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(Observable.EveryUpdate()~AddTo()の返り値まわり対応)
(ReactiveCollectionをObservableListへ)
行61: 行61:
 
==ReactiveCollectionをObservableList<T>へ==
 
==ReactiveCollectionをObservableList<T>へ==
 
ObservableList では、`using ObservableCollections;`を追加する。
 
ObservableList では、`using ObservableCollections;`を追加する。
 +
 
<pre>
 
<pre>
// R3
+
// UniRx
using ObservableCollections;
+
private ReactiveCollection<string> users = new ReactiveCollection<string>();
private ObservableList<string> users = new ObservableList<string>();
+
 
users
 
users
 
     .ObserveAdd()
 
     .ObserveAdd()
行72: 行72:
 
     });
 
     });
  
// UniRx
+
// R3
private ReactiveCollection<string> users = new ReactiveCollection<string>();
+
using ObservableCollections;
 +
private ObservableList<string> users = new ObservableList<string>();
 
users
 
users
 
     .ObserveAdd()
 
     .ObserveAdd()
行80: 行81:
 
         Debug.Log($"[Add]Index={value.Index},Value={value.Value}");
 
         Debug.Log($"[Add]Index={value.Index},Value={value.Value}");
 
     });
 
     });
 +
 
</pre>
 
</pre>
  

2024年4月2日 (火) 14:59時点における版

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));

// R3
Observable.EveryValueChanged(transform, _ => _.Count)
     .Subscribe(count => Debug.Log(count));

参考:https://qiita.com/toRisouP/items/4344fbcba7b7e8d8ce16

Observable.Createの置換

// UniRx
IObservable<int> isCompletedsAsObservable = Observable.Create<int>(observer =>
            {
                return Disposable.Empty;
            });
// R3
Observable<int> isCompletedsAsObservable = Observable.Create<int>(observer =>
            {
                return Disposable.Empty;
            });

IObservableをObservableへ

// UniRx
ObservableCollision2DTrigger landTrigger;
IObservable<Collision2D> landOnCollisionEnterAsObservable = landTrigger.OnCollisionEnter2DAsObservable();

// R3
ObservableCollision2DTrigger landTrigger;
Observable<Collision2D> landOnCollisionEnterAsObservable = landTrigger.OnCollisionEnter2DAsObservable();

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の置換

// R3
Subject<string> subject = new Subject<string>();
subject
    .Subscribe(
        message => Debug.Log("msg1:" + message),
        error => Debug.LogError("Error" + error)
            );

// UniRx
Subject<string> subject = new Subject<string>();
subject
    .Subscribe(
        message => Debug.Log("msg1:" + message),
        error => Debug.LogError("Error" + error)
        () => Debug.Log("Completed")
            );