facebook twitter hatena line email

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

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(DropDownサンプル)
(DropDownサンプル)
行49: 行49:
  
 
==DropDownサンプル==
 
==DropDownサンプル==
 +
 +
AsObservableを使う
 +
<pre>
 +
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);
 +
    }
 +
}
 +
</pre>
 +
 +
ObserveEveryValueChangedを使う
 
<pre>
 
<pre>
 
using UnityEngine;
 
using UnityEngine;

2021年9月6日 (月) 17:54時点における版

UniRxとは

UnityでもReactiveExtensionsを使えるように移植したもの

UniRxインストール

https://assetstore.unity.com/packages/tools/integration/unirx-reactive-extensions-for-unity-17276

メソッド説明

Subscribeはイベント名を入れる

ボタンサンプル

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

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