facebook twitter hatena line email

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

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(TMP_DropDownサンプル)
行1: 行1:
 
==TMP_DropDownサンプル==
 
==TMP_DropDownサンプル==
 +
ObserveEveryValueChangedを使う場合
 
<pre>
 
<pre>
 
using UnityEngine;
 
using UnityEngine;
行13: 行14:
 
         tmpDropdown.ObserveEveryValueChanged(_ => _.value)
 
         tmpDropdown.ObserveEveryValueChanged(_ => _.value)
 
             .Subscribe(index => {
 
             .Subscribe(index => {
 +
                var value = dropdown.options[index].text;
 +
                tmpText.text = value;
 +
            })
 +
            .AddTo(gameObject);
 +
    }
 +
}
 +
</pre>
 +
AsObservableを使う場合
 +
<pre>
 +
using UnityEngine;
 +
using UnityEngine.UI;
 +
using UniRx;
 +
using TMPro;
 +
public class SampleScene : MonoBehaviour
 +
{
 +
    void Start()
 +
    {
 +
        TMP_Dropdown tmpDropdown = GameObject.Find("TmpDropdown").GetComponent<TMP_Dropdown>();
 +
        tmpDropdown.onValueChanged.AsObservable()
 +
            .Subscribe(index =>
 +
            {
 
                 var value = dropdown.options[index].text;
 
                 var value = dropdown.options[index].text;
 
                 tmpText.text = value;
 
                 tmpText.text = value;

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

TMP_DropDownサンプル

ObserveEveryValueChangedを使う場合

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

AsObservableを使う場合

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

OnValueChangedAsObservableを作る形式。別途、下にあるようにUnityUIComponentExtensions.csに、OnValueChangedAsObservableを作る

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

UnityUIComponentExtensions.csへusing TMPro;が追加できない問題

別途、アプリ側のScriptディレクトリに、UnityUIExtensionsを作れば良い。

UnityUIExtensions.cs

sing System;
using TMPro;

namespace UniRx
{
    public static class UnityUIExtensions
    {
        public static IDisposable SubscribeToText(this IObservable<string> source, TextMeshProUGUI text)
        {
            return source.SubscribeWithState(text, (x, t) => t.text = x);
        }

        public static IDisposable SubscribeToText<T>(this IObservable<T> source, TextMeshProUGUI text)
        {
            return source.SubscribeWithState(text, (x, t) => t.text = x.ToString());
        }
#if UNITY_5_3_OR_NEWER
        public static IObservable<int> OnValueChangedAsObservable(this TMP_Dropdown dropdown)
        {
            return Observable.CreateWithState<int, TMP_Dropdown>(dropdown, (d, observer) =>
            {
                observer.OnNext(d.value);
                return d.onValueChanged.AsObservable().Subscribe(observer);
            });
        }
#endif
    }
}

参考:https://github.com/neuecc/UniRx/issues/352