Game视图
Hierarchy视图
在开发中遇到省市选择器,再次提供一个思路。
using UnityEngine; using System.Collections; using UnityEngine.UI; using System.Collections.Generic; public class Test : MonoBehaviour { public Dropdown dropList1; public Dropdown dropList2; public List<string[]> listArray; void Start() { Test1(dropList1, dropList2); //初始化 dropList1.onValueChanged.AddListener(ListenValueChanged); //第一个drop添加事件 } /// <summary> /// 选择第一个drop后第二个drop初始化 /// </summary> /// <param name="index">Index.</param> public void ListenValueChanged(int index) { string[] array1 = new string[] { "河北", "北京", "海南省" }; dropList2.options.Clear(); for (int i = 0; i < listArray[index].Length; i++) { Dropdown.OptionData o1 = new Dropdown.OptionData(); o1.text = listArray[index][i]; dropList2.options.Add(o1); } dropList1.captionText.text = array1[index]; dropList2.captionText.text = listArray[index][0]; } /// <summary> /// 第一个初始化 /// </summary> /// <returns>The test1.</returns> /// <param name="d1">D1.</param> /// <param name="d2">D2.</param> private void Test1(Dropdown d1, Dropdown d2) { d1.options.Clear(); d2.options.Clear(); string[] array1 = new string[] { "河北", "北京", "海南省" }; for (int i = 0; i < array1.Length; i++) { Dropdown.OptionData o1 = new Dropdown.OptionData(); o1.text = array1[i]; dropList1.options.Add(o1); } string[] arrayOne = new string[] { "石家庄", "唐山", "保定", "秦皇岛", "沧州", "承德", "邯郸", "衡水", "廊坊", "邢台", "张家口" }; string[] arrayTwo = new string[] { "东城", "西城", "东城", "海淀", "朝阳", "丰台", "门头沟", "石景山", "房山", "通州", "顺义", "昌平", "大兴", "怀柔", "平谷", "延庆", "密云" }; string[] arrayThree = new string[] { "海口市", "三亚市", "三沙市", "儋州市" }; listArray = new List<string[]>(); listArray.Add(arrayOne); listArray.Add(arrayTwo); listArray.Add(arrayThree); for (int i = 0; i < listArray[0].Length; i++) { Dropdown.OptionData o1 = new Dropdown.OptionData(); o1.text = listArray[0][i]; dropList2.options.Add(o1); } dropList1.captionText.text = array1[0]; dropList2.captionText.text = listArray[0][0]; } }