Unity在输入框中输入文字,下拉列表自动出现带关键字的问题,选中后enter键发出
工具:NGUI v3.12.0
网盘链接:链接:https://pan.baidu.com/s/1DCC4lmzLnedqch_MF4VMYg 提取码:cei8
参考NGUI场景例子:Example 12 - Chat Window
不同·之处在于给input对象多添加了UIPopupList.cs和Options.cs。Options.cs直接挂给input对象。
UIInput.cs修改之处:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Options : MonoBehaviour {
//问题库
public List<string> str = new List<string>();
private UIPopupList popupList;
private UIInput input;
//输入关键字的输入框
public UILabel inputLabel;
//存放选择选项的Label
public UILabel tempLabel;
string tempstr;
string strLabel;
bool isSelect;
// Use this for initialization
void Start () {
popupList = GetComponent<UIPopupList>();
input = GetComponent<UIInput>();
}
// Update is called once per frame
void Update () {
if (inputLabel.text != "New Label")
{
if (inputLabel.text != "" && inputLabel.text != null)
{
AddText(Text(inputLabel.text, str));
if (tempstr != inputLabel.text)
{
//符合条件的自动出现面板
popupList.Show();
}
}
}
//存储上一次的搜索值
tempstr = inputLabel.text;
//更新input中的值
if (strLabel != tempLabel.text)
{
input.value = tempLabel.text;
isSelect = true;
}
//存储上次选项的值
strLabel = tempLabel.text;
if (isSelect&& input.isSelected==false)
{
input.isSelected = true;
StartCoroutine(NotHighlight());
//Debug.Log("这里的:" + input.isSelected);
}
}
//从库中查找结果
public List<string> Text(string _text,List<string> _str)
{
List<string> Result = new List<string>();
foreach(string temp in _str)
{
if (temp.Contains(_text))
{
Result.Add(temp);
}
}
return Result;
}
//更新搜索结果,添加选项
public void AddText(List<string> _res)
{
popupList.items.Clear();
foreach(string temp in _res)
{
popupList.AddItem(temp);
}
}
//选择选项时,等待一帧执行
public IEnumerator NotHighlight()
{
yield return 0;
input.SetNum(false);
isSelect = false;
}
}
设置如下:
最后,即可完成该效果。