Unity-GUI-Dropdown-当下拉框选中值改变时,相应值传给输入框

Unity-GUI-Dropdown-当下拉框选中值改变时,相应值传给输入框

首先,此处下拉框addOption中,保存的时BoxData类型的id
所以,我们需要定义一个BoxData的类,与字典dic

通过遍历字典dic
找到选中id的相应BoxData,
将相对应参数,传给InputFeld

public class BoxData {
    public string id;
    public string box_attribute;
    public int width;
    public int height;
    public int len;

    public static Dictionary<string, BoxData> dic = new Dictionary<string, BoxData>();

	public static void getData(XmlNodeList data)
    {
       
        foreach (XmlElement element in data)
        {
            XmlNodeList list =  element.ChildNodes;
            BoxData data2 = new BoxData();
            foreach (XmlElement e2 in list)
            {
                switch (e2.Name) { 

                    case "box_id":
                        data2.id = e2.InnerText;
                        break;
                    case "width":
                        data2.width = int.Parse(e2.InnerText);
                        break;

                    case "height":
                        data2.height = int.Parse(e2.InnerText);
                        break;


                    case "len":
                        data2.len = int.Parse(e2.InnerText);
                        break;
                
                }

            
            }

            dic.Add(data2.id,data2);
            Debug.Log(data2.ToString());
        }
    
    
    }

    public override string ToString()
    {

        string str = "";
        str += width + "\n";
        str += height + "\n";
        str += len + "\n";
        return str;
    }


}

当下拉值改变,添加监听事件:


//当下拉值改变,添加监听事件
 page.box_Dropdown.onValueChanged.AddListener(delegate { onChangeBoxDropdown(); });
public void onChangeBoxDropdown()
    {
        pressCity = page.box_Dropdown.options[page.box_Dropdown.value].text;
        //pressCity:选中的data_id

        if (page.box_Dropdown.value != 0)
        {

            foreach (BoxData element in BoxData.dic.Values)
            {
                if (element.id == pressCity)
                {
                    page.box_x.text = BoxData.dic[pressCity].width + "";
                    page.box_y.text = BoxData.dic[pressCity].height + "";
                    page.box_z.text = BoxData.dic[pressCity].len + "";
                    page.box_typeNum.text = BoxData.dic[pressCity].id;
                }
            }
        }
        else
        {
            page.box_x.text = "";
            page.box_y.text = "";
            page.box_z.text = "";
            page.box_typeNum.text = "";
        }

        Debug.Log("2222222222222222222222222222222222222222");

    }

你可能感兴趣的:(Unity学习笔记)