Unity/TMPro/Dropdown
提供: 初心者エンジニアの簡易メモ
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using TMPro; public class SettingScene : MonoBehaviour { TMP_Dropdown areaDropdown; List<string> areas; string area; void Start() { AreaDropdownInit(); areaDropdown = this.transform.Find("AreaDropdown").GetComponent<TMP_Dropdown>(); areaDropdown.onValueChanged.AddListener(delegate { AreaDropdownValueChanged(areaDropdown); }); } void AreaDropdownInit() { areas = new List<string>(); 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(); } }