facebook twitter hatena line email

Unity/TMPro/Dropdown

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class SettingScene : MonoBehaviour
{
    void Start()
    {
        AreaDropdownInit();
        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();
    }
}