Unity/TMPro/Dropdown
提供: 初心者エンジニアの簡易メモ
2022年9月4日 (日) 23:30時点におけるAdmin (トーク | 投稿記録)による版 (ページの作成:「<pre> using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using TMPro; public class SettingScene : MonoBehaviour { vo...」)
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using TMPro; public class SettingScene : MonoBehaviour { void Start() { InitAreaDropdown(); areaDropdown = this.transform.Find("AreaDropdown").GetComponent<TMP_Dropdown>(); areaDropdown.onValueChanged.AddListener(delegate { AreaDropdownValueChanged(areaDropdown); }); } void AreaDropdownInit() { areas.Add("simple"); areas.Add("sabaku"); areas.Add("sushi"); List<TMP_Dropdown.OptionData> optionMessages = new List<TMP_Dropdown.OptionData>(); TMP_Dropdown areaDropdown = this.transform.Find("AreaDropdown").GetComponent<TMP_Dropdown>(); areaDropdown.ClearOptions(); foreach (string area in areas) { TMP_Dropdown.OptionData optionData; optionData = new TMP_Dropdown.OptionData(); optionData.text = area; optionMessages.Add(optionData); } foreach (TMP_Dropdown.OptionData message in optionMessages) { areaDropdown.options.Add(message); } areaDropdown.value = areas.IndexOf(area); this.transform.Find("AreaDropdown/Label").GetComponent<TextMeshProUGUI>().text = areas[areaDropdown.value].ToString(); } void AreaDropdownValueChanged(TMP_Dropdown change) { area = areas[change.value]; this.transform.Find("AreaDropdown/Label").GetComponent<TextMeshProUGUI>().text = areas[areaDropdown.value].ToString(); } }