LoginSignup
4
4

More than 1 year has passed since last update.

UniRxのUIにまつわる処理3選

Last updated at Posted at 2021-01-24

様々な使い方のあるUnityのライブラリ「UniRx」の中でも、
UIにまつわる処理を紹介します。

その1. ReactiveProperty
その2. BindToButtonOnClick
その3. BindToOnClick

その1. ReactiveProperty

Sample1
using UnityEngine;
using UniRx;

public class Sample1 : MonoBehaviour
{
    //int level; とほぼ同じ使い方ができる。
    ReactiveProperty<int> level = new ReactiveProperty<int>();

    void Start()
    {
        level.Subscribe(Talk);
        //level.Subscribe(arg => Talk(arg)); のようなラムダ式でも可
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            level.Value++;  //.Valueで中身にアクセスする
        }
    }

    void Talk(int level)
    {
        Debug.Log("level up!" + level);
    }
}

Spaceを押すとlevelが上がり、levelが上がると同時にlevel up!と表示される。

ReactivePropertyは、ざっくり言うと、値の変更を通知してくれる変数のようなものです。
new ReactiveProperty()で作成し、Valueから値を取得・設定します。
(UniRxのReactivePropertyについて より引用。詳しい使い方も書かれています。)

Subscribe関数から、値の変更時に呼ばれて欲しい関数を登録することができます。
値と常に同期して欲しいことの多いUIの変更に便利です。

その2. BindToButtonOnClick

Sample2
using UnityEngine;
using UnityEngine.UI;
using UniRx;

public class UniRxSample2 : MonoBehaviour
{
    [SerializeField] Button button;

    //bool can_press; とほぼ同じ使い方ができる。
    ReactiveProperty<bool> can_press = new ReactiveProperty<bool>();

    // Start is called before the first frame update
    void Start()
    {
        can_press.BindToButtonOnClick(button, _ => Click());
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.T))
        {
            can_press.Value = true;
        }

        if (Input.GetKeyDown(KeyCode.F))
        {
            can_press.Value = false;
        }
    }

    void Click() { Debug.Log("Click"); }
}

can_pressというReactivePropertyの値が、Tキーを押すとtrueに、Fキーを押すとfalseに変更される。can_pressの値が変更されるとbuttonのinteractableも変更される。

BindToButtonOnClickは、ボタンに"押下された際の処理"と"押せるかどうか"、つまりonClickとinteractableを同時に登録するような関数です。
bool型のReactivePropertyから呼び出すことができます。

ちなみに、can_press.SubscribeToInteractableでinteractableのみ登録ができます。

ButtonのOnClickAsObservable()関数は、onClickをIObservableに変換する関数です。
Where関数で、bool型の関数を入力することができます。そのbool型の関数の結果がtrueの場合のみしか登録した関数を呼びださない、という処理が可能になります。

その3. BindToOnClick

Sample3
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UniRx;

public class UniRxSample3 : MonoBehaviour
{
    [SerializeField] private Button button;

    void Start()
    {
        button.BindToOnClick(_ => Observable.FromCoroutine(LevelingUp));
    }

    IEnumerator LevelingUp()
    {
        int level = 0;
        for (int i = 0; i < 100; i++)
        {
            level++;
            yield return null;
        }
        Debug.Log("level up!");
    }
}

buttonを押すと、コルーチンが呼ばれ、完了するまでButtonのinteractableがfalseとなる。

BindToOnClickはButtonにIObservableを返す関数を登録することができる関数です。
入力したIObservableが完了するまで、interactableがfalseとなります。
アニメーションを伴う処理や、サーバーと通信する処理などをする間、Buttonを押せなくするのに便利です。
CoroutineはFromCoroutine関数でIObservableに変換することができます。

4
4
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4