using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class DropdownListData : MonoBehaviour { private Dropdown ThisDropdown; public string SelectText { get { return ThisDropdown.captionText.text; } set { SetDropdownValue(value); } } void Awake() { ThisDropdown = this.GetComponent(); } public void AddData(string data) { Dropdown.OptionData tempData = new Dropdown.OptionData(); tempData.text = data; ThisDropdown.options.Add(tempData); } public void DataBind(List List) { ThisDropdown.ClearOptions(); Dropdown.OptionData tempData; if (List == null || List.Count <= 0) { ThisDropdown.captionText.text = string.Empty; return; } for (int i = 0; i < List.Count; i++) { tempData = new Dropdown.OptionData(); tempData.text = List[i]; ThisDropdown.options.Add(tempData); } ThisDropdown.value = 0; ThisDropdown.captionText.text = ThisDropdown.options[0].text; } public void SetDropdownValue(string text) { int index = ThisDropdown.options.FindIndex((x) => { return x.text == text; }); if (index != -1) { ThisDropdown.value = index; ThisDropdown.captionText.text = text; } else ThisDropdown.captionText.text = text; } }