Dropdown(Script)
Interactable | 可交互 | 是否可点击 |
Transition | 过渡 |
|
Navigation | 导航 |
|
Template | 模板 | 下拉使用的模板样式 |
Caption Text | 标题文本 | 当前选择的显示文本 |
Caption Image | 标题图像 | 当前选择的显示图片 |
Item Text | 项文本 | 下拉框选项模板里的文字,不填会导致选项列表中的每个选项不会修改模板里的文字 |
Item Image | 项图像 | 下拉框选项模板里的图片,不填会导致选项列表中的每个选项不会修改模板里的图片 |
Value | 值 | 当前选择的值(0开始) |
Options | 选项 | 可选择项 |
On Value Changed(Int32) | 值改变时(32位整数) | 值调整时触发的事件 |
自带Dropdown
Dropdown:图片,带Dropdown组件,作为选中框的底图。
Label:文本,作为标题文本,当前显示的文本。
Arrow:图片,作为下拉按钮。
运行时:
绑定事件
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TestUI : MonoBehaviour
{
Dropdown dropdown;
void Start()
{
dropdown = GameObject.Find("Canvas/Dropdown").GetComponent<Dropdown>();
}
public void GetValue(int i)
{
switch (i)
{
case 0: Debug.Log("Option A"); break;
case 1: Debug.Log("Option B"); break;
case 2: Debug.Log("Option C"); break;
case 3: Debug.Log("Option D"); break;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TesUI : MonoBehaviour
{
public string[] showText;
public Sprite[] sprite;
Dropdown dropDownItem;
List<string> temoNames;
List<Sprite> sprite_list;
void Start()
{
dropDownItem = this.GetComponent<Dropdown>();
temoNames = new List<string>();
sprite_list = new List<Sprite>();
AddNames();
UpdateDropDownItem(temoNames);
}
void UpdateDropDownItem(List<string> showNames)
{
dropDownItem.options.Clear();
Dropdown.OptionData temoData;
for (int i = 0; i < showNames.Count; i++)
{
temoData = new Dropdown.OptionData();
temoData.text = showNames[i];
temoData.image = sprite_list[i];
dropDownItem.options.Add(temoData);
}
dropDownItem.captionText.text = showNames[0];
}
void AddNames()
{
for (int i = 0; i < showText.Length; i++)
{
temoNames.Add(showText[i]);
}
for (int i = 0; i < sprite.Length; i++)
{
sprite_list.Add(sprite[i]);
}
}
}