本文要解决的问题:
1、如何用代码初始化下拉菜单的item
2、如何乱序加载item
3、如何读取当前选中的item的值
一、下拉框外形——Dropdown 二、脚本的配置 三、功能实现 1、初始化Dropdown的下拉列表
///2、乱序排列一个列表/// 增加一个【item菜单】 /// /// void AddItmes(string strName) { //Make the index the last number of entries m_Index = m_Messages.Count; //Create a temporary option Dropdown.OptionData temp = new Dropdown.OptionData(); //Make the option the data from the TextField temp.text = strName; //Update the messages list with the TextField data m_Messages.Add(temp); //Add the Textfield data to the Dropdown m_Dropdown.options.Insert(m_Index, temp); }
///3、读取当前的item/// 打乱list的item顺序 /// ////// /// private List RandomSort (List list) { var random = new System.Random(); var newList = new List (); foreach (var item in list) { newList.Insert(random.Next(newList.Count), item); } return newList; }
关键点:
获取当前项:
currentItem = GetComponent().options[idx].text
当前项的idx从哪里来:
idx = GetComponent().value
///四、代码清单/// onValueChanged绑定的方法:读取所选择的值 /// void SetSelectedItem() { var dp = dropDownGo.GetComponent(); var idx = dp.value; selectedItem = dp.options[idx].text; //TXDebug.Log(selectedItem); }
using UnityEngine; using UnityEngine.UI; using System.Collections.Generic; using System.Linq; using System; ////// 【下拉列表】控件的配套脚本 /// 1、初始化下拉清单的值:面板上预先给定一个格式化的字符串,格式【苹果#香蕉#...】 /// 特殊功能:使用【随机顺序】初始化下拉框 /// /// 2、暴露当前选中的值,供外部调用 /// 备注:该脚本原型来自Unity官网demo /// public class DropDownController : MonoBehaviour { ////// 挂载dropdown组件的Gameobject /// [Header("挂载dropdown组件的Gameobject")] [SerializeField] public GameObject dropDownGo; ////// 下拉菜单的品种,格式:【item1#item2#...】 /// [Header("备选的选项【选项1#选项2#...】")] [SerializeField] public string items; ////// 是否乱序加载:下拉框中的下拉元素是否乱序加载 /// [Header("是否乱序加载")] [SerializeField] public bool randomSort; ////// 当前被选中的项:string /// [Header("当前被选中的项")] [SerializeField] public string selectedItem; //Use these for adding options to the Dropdown List Dropdown.OptionData m_NewData, m_NewData2; //The list of messages for the Dropdown Listm_Messages = new List (); //This is the Dropdown Dropdown m_Dropdown; string m_MyString; int m_Index; private void OnEnable() { dropDownGo.GetComponent ().onValueChanged.AddListener(delegate { SetSelectedItem(); }); } void Start() { //Fetch the Dropdown GameObject the script is attached to m_Dropdown = dropDownGo.GetComponent (); //Clear the old options of the Dropdown menu m_Dropdown.ClearOptions(); //加载下拉元素:乱序或者正常顺序 var finItems = randomSort ? RandomSort(items.Split('#').ToList()).ToArray() : items.Split('#'); LoadItems(finItems);//初始化的时候,为空 selectedItem = finItems[0]; } /// /// 增加一个【item菜单】 /// /// void AddItmes(string strName) { //Make the index the last number of entries m_Index = m_Messages.Count; //Create a temporary option Dropdown.OptionData temp = new Dropdown.OptionData(); //Make the option the data from the TextField temp.text = strName; //Update the messages list with the TextField data m_Messages.Add(temp); //Add the Textfield data to the Dropdown m_Dropdown.options.Insert(m_Index, temp); } ////// 加载所有item /// /// 字符串数组 void LoadItems(string[] ary) { ary.ToList().ForEach(x=>AddItmes(x)); } //private void Update() //{ // if (Input.GetKeyDown(KeyCode.Return)) // { // //生成乱序的列表 ----测试---- // TXDebug.Log("-------------------------------------------"); // items.Split('#').ToList().ForEach(x => TXDebug.Log(x)); // TXDebug.Log("-------------------------------------------"); // RandomSort(items.Split('#').ToList()).ForEach(x => TXDebug.Log(x)); // } //} ////// onValueChanged绑定的方法:读取所选择的值 /// void SetSelectedItem() { var dp = dropDownGo.GetComponent(); var idx = dp.value; selectedItem = dp.options[idx].text; //TXDebug.Log(selectedItem); } /// /// 打乱list的item顺序 /// ////// /// private List RandomSort (List list) { var random = new System.Random(); var newList = new List (); foreach (var item in list) { newList.Insert(random.Next(newList.Count), item); } return newList; } }



