facebook twitter hatena line email

「Unity/UniRx/AsObservable」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
行1: 行1:
==UniRxとは==
 
UnityでもReactiveExtensionsを使えるように移植したもの
 
 
==UniRxインストール==
 
https://assetstore.unity.com/packages/tools/integration/unirx-reactive-extensions-for-unity-17276
 
  
 
==メソッド説明==
 
==メソッド説明==

2021年9月7日 (火) 09:33時点における版

メソッド説明

Subscribeは、イベント名を入れる AddToは、gameobjectが、使われなくなったら自動破棄してくれる。

ボタンサンプル

ボタンクリックで、カウントアップする

AsObservableサンプル

using UnityEngine;
using UnityEngine.UI;
using UniRx;
public class SampleScene : MonoBehaviour
{
    void Start()
    {
        Button button = GameObject.Find("Button").GetComponent<Button>();
        Text text = GameObject.Find("Text").GetComponent<Text>();
        button.onClick.AsObservable()
            .Select(_ => 1)
            .Scan(0, (element, acc) => element + acc)
            .Subscribe (count => text.text = count.ToString ())
            .AddTo(gameObject);
    }
}

OnClickAsObservableサンプル

public class SampleScene : MonoBehaviour
{
    void Start()
    {
        Button button = GameObject.Find("Button").GetComponent<Button>();
        Text text = GameObject.Find("Text").GetComponent<Text>();
        button.OnClickAsObservable()
            .Select(_ => 1)
            .Scan(0, (element, acc) => element + acc)
            .SubscribeToText(text)
            .AddTo(gameObject);
    }
}

DropDownサンプル

AsObservableを使う

using UnityEngine;
using UnityEngine.UI;
using UniRx;
public class SampleScene : MonoBehaviour
{
    void Start()
    {
         Text text = GameObject.Find("Text").GetComponent<Text>();
         Dropdown dropdown = GameObject.Find("Dropdown").GetComponent<Dropdown>();
        dropdown.onValueChanged.AsObservable()
            .Subscribe(index =>
            {
                var value = dropdown.options[index].text;
                text.text = value;
            })
            .AddTo(gameObject);
    }
}

ObserveEveryValueChangedを使う

using UnityEngine;
using UnityEngine.UI;
using UniRx;
public class SampleScene : MonoBehaviour
{
    void Start()
    {
         Text text = GameObject.Find("Text").GetComponent<Text>();
         Dropdown dropdown = GameObject.Find("Dropdown").GetComponent<Dropdown>();
         dropdown.ObserveEveryValueChanged(_ => _.value)
             .Subscribe(index => {
                var value = dropdown.options[index].text;
                text.text = value;
             })
            .AddTo(gameObject);
    }
}

OnValueChangedAsObservableを使う場合

using UnityEngine;
using UnityEngine.UI;
using UniRx;
public class SampleScene : MonoBehaviour
{
    void Start()
    {
         Text text = GameObject.Find("Text").GetComponent<Text>();
         Dropdown dropdown = GameObject.Find("Dropdown").GetComponent<Dropdown>();
        dropdown.OnValueChangedAsObservable()
            .Subscribe(index =>
            {
                var value = dropdown.options[index].text;
                text.text = value;
            })
            .AddTo(gameObject);
    }
}

参考

UniRxでボタンのクリック回数をテキストに表示する https://qiita.com/RyotaMurohoshi/items/01df35f1940e93fbb10d

【Unity】UniRxを使ってuGUIのイベントを監視する https://tm8r.hateblo.jp/entry/2016/05/13/203922