栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > 其他

Unity 下拉菜单中【下拉列表自动加载】 以及 【读取当前item的值】

其他 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Unity 下拉菜单中【下拉列表自动加载】 以及 【读取当前item的值】

本文要解决的问题:


1、如何用代码初始化下拉菜单的item
2、如何乱序加载item
3、如何读取当前选中的item的值


一、下拉框外形——Dropdown

二、脚本的配置

三、功能实现 1、初始化Dropdown的下拉列表
/// 
/// 增加一个【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);
 }
2、乱序排列一个列表
/// 
/// 打乱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;
}
3、读取当前的item

关键点:
获取当前项:
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
    List m_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;
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/902143.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号