当前位置:网站首页>Unity Dropdown(可编辑,可输入)下拉选择框,带文本联想
Unity Dropdown(可编辑,可输入)下拉选择框,带文本联想
2022-07-17 13:48:00 【暗夜__】

前 言
由于 Unity 没有直接实现该效果的组件,所以以上效果是用Dropdown和InputField这个两个组件 组合实现的。
UI布局

核心代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SearchBar : MonoBehaviour
{
public Dropdown dropdown;
public InputField inputField;
/// <summary>
/// 下拉列表的默认 初始数据列表
/// </summary>
public List<string> LibraryList = new List<string>();
/// <summary>
/// 搜索后 返回的数据列表
/// </summary>
private List<string> ResultList = new List<string>();
private void Start()
{
Init();
InitDropDown();
dropdown.onValueChanged.AddListener(delegate
{
Debug.Log(dropdown.transform.Find("Label").GetComponent<Text>().text);
inputField.text = dropdown.transform.Find("Label").GetComponent<Text>().text;
HideInputField();
});
inputField.onEndEdit.AddListener(delegate
{
Filter();
ShowResult();
});
}
private void Update()
{
if (inputField.isFocused && inputField.placeholder.gameObject.activeSelf == false)
{
ShowInputField();
}
}
private void Init()
{
LibraryList.ForEach(i => ResultList.Add(i));//括号内是一个lambda表达式,i所代表的是LibraryList集合中的一个元素
SetPlaceholder("请输入...");
}
/// <summary>
/// 初始化 下拉列表
/// </summary>
private void InitDropDown()
{
dropdown.ClearOptions();//清空
dropdown.options.Add(new Dropdown.OptionData());
foreach (var item in LibraryList)
{
Dropdown.OptionData optionData = new Dropdown.OptionData();
optionData.text = item;
dropdown.options.Add(optionData);
}
}
private void HideInputField()
{
inputField.GetComponent<Image>().color = new Color(1,1,1,0);
inputField.placeholder.gameObject.SetActive(false);
inputField.textComponent.gameObject.SetActive(false);
}
private void ShowInputField()
{
inputField.GetComponent<Image>().color = new Color(1,1,1,1);
inputField.placeholder.gameObject.SetActive(true);
inputField.textComponent.gameObject.SetActive(true);
}
/// <summary>
/// 设置默认显示文字
/// </summary>
/// <param name="str"></param>
public void SetPlaceholder(string str)
{
inputField.placeholder.GetComponent<Text>().text = str;
}
/// <summary>
/// 设置下拉列表的默认数据
/// </summary>
/// <param name="_list"></param>
public void SetLibraryList(List<string> _list)
{
LibraryList = _list;
}
/// <summary>
/// 筛选字符
/// </summary>
private void Filter()
{
ResultList = TextLenovo(inputField.textComponent.text, LibraryList);
}
/// <summary>
/// 显示搜索结果
/// </summary>
private void ShowResult()
{
dropdown.ClearOptions();
dropdown.AddOptions(ResultList);
if (ResultList.Count != 0)
{
dropdown.Show();
}
}
/// <summary>
/// 文本联想
/// </summary>
/// <param 传入的字符="text_item"></param>
/// <param 库数组="TextLibraryList"></param>
/// <param 返回结果数组="temp_list"></param>
/// <returns></returns>
public List<string> TextLenovo(string text_item, List<string> TextLibraryList)
{
List<string> temp_list = new List<string>();
foreach (string item in TextLibraryList)
{
if (item.Contains(text_item))
{
temp_list.Add(item);
}
}
return temp_list;
}
}
项目Doem 源码GitEE 地址:https://gitee.com/Andy_Yi/drop-down-dome
边栏推荐
猜你喜欢

37. Flex layout

Common collection properties

leetcode-08

Pytorch框架 学习记录1 CIFAR-10分类

Pytorch与权重衰减(L2范数)

LeetCode 2319. Judge whether the matrix is an X matrix

Antd drop-down multiple options to transfer values to the background for query operations

剑指 Offer II 041. 滑动窗口的平均值

LeetCode 745. 前缀和后缀搜索

antd 下拉多选传值到后台做查询操作
随机推荐
Google Earth Engine——Hansen Global Forest Change v1.8 (2000-2020) 森林覆盖度和森林损失量数据集
Journal日志与oplog日志的区别
[leetcode weekly replay] 302 weekly 20220717
军品研制过程所需文件-进阶版
win10开始键点击无响应
如何在双链笔记软件中建立仪表盘和知识库?以嵌入式小组件库 NotionPet 为例
Summary of port mirroring methods with VDS or NSX under vSphere
Vérification logique complexe personnalisée lors de l'ajout et de la modification - 2022 nouvel élément
如何在 RHEL 9 中更改和重置忘记的root密码
今日睡眠质量记录79分
Pytorch学习记录2 线性回归(Tensor,Variable)
新增、修改操作时自定义复杂逻辑校验-2022新项目
Explanation of tree chain dissection idea + acwing 2568 Tree chain dissection (DFS sequence + mountain climbing method + segment tree)
Custom complex logic verification during adding and modifying -2022 new project
[Huawei cloud IOT] reading notes, "Internet of things: core technology and security of the Internet of things", Chapter 3 (2)
Thinking about the integrated communication of air, space and earth based on the "7.20 Zhengzhou rainstorm"
反向散射通信的未来应用与技术挑战
过拟合与欠拟合
Satellite network capacity improvement method based on network coding
Integrated network architecture and network slicing technology of air, earth and sea