「Unity/UniRx/TMPro」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→UnityUIComponentExtensions.csへusing TMPro;が追加できない問題) |
(→TMP_DropDownサンプル) |
||
行9: | 行9: | ||
void Start() | void Start() | ||
{ | { | ||
− | + | TextMeshProUGUI tmpText = GameObject.Find("Text (TMP)").GetComponent<TextMeshProUGUI>(); | |
TMP_Dropdown tmpDropdown = GameObject.Find("TmpDropdown").GetComponent<TMP_Dropdown>(); | TMP_Dropdown tmpDropdown = GameObject.Find("TmpDropdown").GetComponent<TMP_Dropdown>(); | ||
tmpDropdown.ObserveEveryValueChanged(_ => _.value) | tmpDropdown.ObserveEveryValueChanged(_ => _.value) | ||
.Subscribe(index => { | .Subscribe(index => { | ||
var value = dropdown.options[index].text; | var value = dropdown.options[index].text; | ||
− | + | tmpText.text = value; | |
}) | }) | ||
.AddTo(gameObject); | .AddTo(gameObject); | ||
行20: | 行20: | ||
} | } | ||
</pre> | </pre> | ||
+ | |||
+ | OnValueChangedAsObservableを作る形式。別途下にあるようにcsを作る | ||
+ | <pre> | ||
+ | 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;が追加できない問題== | ==UnityUIComponentExtensions.csへusing TMPro;が追加できない問題== |
2021年9月6日 (月) 18:02時点における版
TMP_DropDownサンプル
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); } }
OnValueChangedAsObservableを作る形式。別途下にあるようにcsを作る
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 <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 } }