Unity(可编辑,可输入)下拉选择框,带文本联想。

先来两个效果展示图

 

 

Unity(可编辑,可输入)下拉选择框,带文本联想。_第1张图片  Unity(可编辑,可输入)下拉选择框,带文本联想。_第2张图片

 

由于Unity没有一个组件直接支持这个效果,所以以上效果是用两个组件通过一些小技巧结合拼起来的。

使用工具:Unity2018.2.18f1
使用组件:

1.UGUI --- Dropdown(下拉框)

2.UGUI --- InputField(输入框)

看下UI布局:

废话不多说直接干货上代码,此脚本挂在SearchBar上。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class SearchBar : MonoBehaviour
{
    private Dropdown My_dorpdown
    {
        get
        {
            return transform.Find("Dropdown").gameObject.GetComponent();
        }
    }
    private InputField My_inputField
    {
        get
        {
            return transform.Find("InputField").gameObject.GetComponent();
        }
    }

    private void HideInputField()
    {
        My_inputField.GetComponent().color = new Color(1, 1, 1, 0);
        My_inputField.placeholder.gameObject.SetActive(false);
        My_inputField.textComponent.gameObject.SetActive(false);
    }

    private void ShowInputField()
    {
        My_inputField.GetComponent().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;
    }

    public List LibraryList = new List();
    public void SetLibraryList(List _list)
    {
        LibraryList = _list;
    }
    private List ResultList = new List();

    private void Start()
    {
        Init();
        My_dorpdown.onValueChanged.AddListener(delegate
        {
            My_inputField.text = My_dorpdown.transform.Find("Label").GetComponent().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.ClearOptions();
        My_dorpdown.AddOptions(ResultList);
        if (ResultList.Count != 0)
        {
            My_dorpdown.Show();
        }
    }
}

文本联想:

        /// 
        /// 文本联想
        /// 
        /// 
        /// 
        /// 
        /// 
        public List TextLenovo(string text_item, List TextLibraryList)
        {
            List temp_list = new List();
            foreach (string item in TextLibraryList)
            {
                if (item.Contains(text_item))
                {
                    temp_list.Add(item);
                }
            }
            return temp_list;
        }

 

你可能感兴趣的:(Unity(可编辑,可输入)下拉选择框,带文本联想。)