facebook twitter hatena line email

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

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(ボタンサンプル)
(ボタンサンプル)
行21: 行21:
 
             .Select(_ => 1)
 
             .Select(_ => 1)
 
             .Scan(0, (element, acc) => element + acc)
 
             .Scan(0, (element, acc) => element + acc)
             .Subscribe (count => text.text = count.ToString ())
+
             .Subscribe (count => text.text = count.ToString())
 
             .AddTo(gameObject);
 
             .AddTo(gameObject);
 
     }
 
     }

2021年10月15日 (金) 00:52時点における版

メソッド説明

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

Sliderを使う場合

Slider slider = GameObject.Find("Slider").GetComponent<Slider>();
slider.OnValueChangedAsObservable()
    .Subscribe(index =>
    {
        // float value = slider.value;
        float value = index;
        text.text = value.ToString();
    })
    .AddTo(gameObject);

有効無効切り替え

.Whereに、trueかfalseを入れる。

enabled = true;
button.OnClickAsObservable()
            .Where(_ => enabled)
            // .Where(_ => 1 == 1) // 有効
            // .Where(_ => 1 == 2) // 無効
            .Select(_ => 1)
            .Scan(0, (element, acc) => element + acc)
            .SubscribeToText(text)
            .AddTo(gameObject);

SelectやDistinctなどがある。以下参照。

Unity/UniRx/Subject [ショートカット]

参考

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

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