本文要解决的问题:
1、如何用代码初始化下拉菜单的item
2、如何乱序加载item
3、如何读取当前选中的item的值
一、下拉框外形——Dropdown
二、脚本的配置
三、功能实现
1、初始化Dropdown的下拉列表
/// <summary>
/// 增加一个【item菜单】
/// </summary>
/// <param name="strName"></param>
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);
}
2、乱序排列一个列表
/// <summary>
/// 打乱list的item顺序
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <returns></returns>
private List<T> RandomSort<T>(List<T> list)
{
var random = new System.Random();
var newList = new List<T>();
foreach (var item in list)
{
newList.Insert(random.Next(newList.Count), item);
}
return newList;
}
3、读取当前的item
关键点:
获取当前项:
currentItem = GetComponent().options[idx].text
当前项的idx从哪里来:
idx = GetComponent().value
/// <summary>
/// onValueChanged绑定的方法:读取所选择的值
/// </summary>
void SetSelectedItem()
{
var dp = dropDownGo.GetComponent<Dropdown>();
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;
/// <summary>
/// 【下拉列表】控件的配套脚本
/// 1、初始化下拉清单的值:面板上预先给定一个格式化的字符串,格式【苹果#香蕉#...】
/// 特殊功能:使用【随机顺序】初始化下拉框
///
/// 2、暴露当前选中的值,供外部调用
/// 备注:该脚本原型来自Unity官网demo
/// </summary>
public class DropDownController : MonoBehaviour
{
/// <summary>
/// 挂载dropdown组件的Gameobject
/// </summary>
[Header("挂载dropdown组件的Gameobject")]
[SerializeField]
public GameObject dropDownGo;
/// <summary>
/// 下拉菜单的品种,格式:【item1#item2#...】
/// </summary>
[Header("备选的选项【选项1#选项2#...】")]
[SerializeField]
public string items;
/// <summary>
/// 是否乱序加载:下拉框中的下拉元素是否乱序加载
/// </summary>
[Header("是否乱序加载")]
[SerializeField]
public bool randomSort;
/// <summary>
/// 当前被选中的项:string
/// </summary>
[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
List<Dropdown.OptionData> m_Messages = new List<Dropdown.OptionData>();
//This is the Dropdown
Dropdown m_Dropdown;
string m_MyString;
int m_Index;
private void OnEnable()
{
dropDownGo.GetComponent<Dropdown>().onValueChanged.AddListener(delegate { SetSelectedItem(); });
}
void Start()
{
//Fetch the Dropdown GameObject the script is attached to
m_Dropdown = dropDownGo.GetComponent<Dropdown>();
//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];
}
/// <summary>
/// 增加一个【item菜单】
/// </summary>
/// <param name="strName"></param>
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);
}
/// <summary>
/// 加载所有item
/// </summary>
/// <param name="ary">字符串数组</param>
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));
// }
//}
/// <summary>
/// onValueChanged绑定的方法:读取所选择的值
/// </summary>
void SetSelectedItem()
{
var dp = dropDownGo.GetComponent<Dropdown>();
var idx = dp.value;
selectedItem = dp.options[idx].text;
//TXDebug.Log(selectedItem);
}
/// <summary>
/// 打乱list的item顺序
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <returns></returns>
private List<T> RandomSort<T>(List<T> list)
{
var random = new System.Random();
var newList = new List<T>();
foreach (var item in list)
{
newList.Insert(random.Next(newList.Count), item);
}
return newList;
}
}