「Unity/UniRx/TMPro」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→TMP_DropDownサンプル) |
(→UnityUIComponentExtensions.csへusing TMPro;が追加できない問題) |
||
行22: | 行22: | ||
==UnityUIComponentExtensions.csへusing TMPro;が追加できない問題== | ==UnityUIComponentExtensions.csへusing TMPro;が追加できない問題== | ||
− | + | 別途、アプリ側のScriptディレクトリに、UnityUIExtensionsを作れば良い。 | |
+ | |||
+ | UnityUIExtensions.cs | ||
+ | <pre> | ||
+ | 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 | ||
+ | } | ||
+ | } | ||
+ | </pre> | ||
参考:https://github.com/neuecc/UniRx/issues/352 | 参考:https://github.com/neuecc/UniRx/issues/352 |
2021年9月6日 (月) 18:01時点における版
TMP_DropDownサンプル
using UnityEngine; using UnityEngine.UI; using UniRx; using TMPro; public class SampleScene : MonoBehaviour { void Start() { Text text = GameObject.Find("Text").GetComponent<Text>(); TMP_Dropdown tmpDropdown = GameObject.Find("TmpDropdown").GetComponent<TMP_Dropdown>(); tmpDropdown.ObserveEveryValueChanged(_ => _.value) .Subscribe(index => { var value = dropdown.options[index].text; text.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 } }