using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class Xialashurupjbh : MonoBehaviour
{
// Start is called before the first frame update
private Dropdown My_dorpdown
{
get
{
return transform.Find("pjbh").gameObject.GetComponent<Dropdown>();
}
}
private InputField My_inputField
{
get
{
return transform.Find("InputFieldpjbh").gameObject.GetComponent<InputField>();
}
}
private void HideInputField()
{
My_inputField.GetComponent<Image>().color = new Color(1, 1, 1, 0);
My_inputField.placeholder.gameObject.SetActive(false);
My_inputField.textComponent.gameObject.SetActive(false);
My_dorpdown.transform.Find("Label").gameObject.SetActive(true);
}
private void ShowInputField()
{
My_inputField.GetComponent<Image>().color = new Color(1, 1, 1, 1);
My_inputField.placeholder.gameObject.SetActive(true);
My_inputField.textComponent.gameObject.SetActive(true);
}
public void SetPlaceholder(string _text)
{
My_inputField.placeholder.GetComponent<Text>().text = _text;
}
public List<string> LibraryList = new List<string>();
public void SetLibraryList(List<string> _list)
{
LibraryList = _list;
}
private List<string> ResultList = new List<string>();
private void Start()
{
Init();
My_dorpdown.onValueChanged.AddListener(delegate
{
My_inputField.text = My_dorpdown.transform.Find("Label").GetComponent<Text>().text;
HideInputField();
});
My_inputField.onEndEdit.AddListener(delegate
{
Filter();
ShowResult();
});
}
private void Init()
{
LibraryList.ForEach(i => ResultList.Add(i));
SetPlaceholder("");
}
private void Update()
{
if (My_inputField.isFocused && My_inputField.placeholder.gameObject.activeSelf == false)
{
ShowInputField();
}
}
//筛选字符
private void Filter()
{
ResultList = TextLenovo(My_inputField.textComponent.text, LibraryList);
}
private void ShowResult()
{
// 下拉框透明
My_dorpdown.transform.Find("Label").gameObject.SetActive(false);
My_dorpdown.AddOptions(ResultList);
if (ResultList.Count != 0)
{
My_dorpdown.Show();
}
My_dorpdown.gameObject.SetActive(true);
}
/// <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;
}
}
本文介绍了一个Unity UI组件的实现,通过结合下拉框和输入框,实现了输入联想功能。当用户在输入框中输入文字时,系统会从预设的库中筛选出包含该文字的所有选项,并显示在下拉框中供用户选择。

2895

被折叠的 条评论
为什么被折叠?



