Unity下拉框可自由选择可输入

本文介绍了一个Unity UI组件的实现,通过结合下拉框和输入框,实现了输入联想功能。当用户在输入框中输入文字时,系统会从预设的库中筛选出包含该文字的所有选项,并显示在下拉框中供用户选择。

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;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值