我正在尝试创建一个动态生成的列表,当用户键入关键字(例如“ The”)时,将出现所有在书名中包含单词“ The”的书籍的结果集。我希望每个结果都可以统一链接到新场景,单击列表项后,它将在Unity中打开它链接到的场景。我看过很多关于按钮的教程,但是关于文本框的教程不多?
我已经尝试过在GetMouseButtonDown上使用IF语句,但是我99%的肯定那不是解决问题的正确方法。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Linq;
public class SearchBarMain : MonoBehaviour
{
public string searchTerm;
public GameObject inputField;
public GameObject textDisplay;
public string SceneName;
public void Search() {
searchTerm = inputField.GetComponent<Text>().text;
List<string> Books= new List<string>() {
"The Count of Monte Cristo",
"Jayne Eire",
"The Black Sheep",
"In Search of Lost Time"
};
var result = Books.Where(str => str.IndexOf(searchTerm, System.StringComparison.OrdinalIgnoreCase) >= 0).ToList();
textDisplay.GetComponent<Text>().text = "";
if (result.Count() > 0)
{
foreach (var item in result)
SceneName = item.ToString();
textDisplay.GetComponent<Text>().text += SceneName + " \n";
if (Input.GetMouseButtonDown(0))
{
SceneManager.LoadScene(SceneName);
}
}
else
{
textDisplay.GetComponent<Text>().text = "None found, search again";
}
}
void Start()
{ }
}