Unity 简单搜索功能

Unity 简单搜索功能
在这里插入图片描述

    public InputField inputField;
	public RectTransform parent;
	public RectTransform item;

	public string[] content;
	void Start()
	{
		inputField.onValueChanged.AddListener((value) =>
		{
			if (parent.childCount > 1)
			{
				for (int i = 1; i < parent.childCount; i++)
				{
					DestroyImmediate(parent.GetChild(i).gameObject);
				}
			}

			Debug.Log("value==" + value);
			if (!string.IsNullOrEmpty(value))
			{
				if (parent.childCount > 1)
				{
					for (int i = 1; i < parent.childCount; i++)
					{
						DestroyImmediate(parent.GetChild(i).gameObject);
					}
				}
				foreach (var ch in content)
				{
				//关键比对代码
					if (ch.IndexOf(value, StringComparison.OrdinalIgnoreCase) >= 0)
					{
						RectTransform rt = Instantiate(item, parent);
						rt.GetChild(0).GetComponent<Text>().text = ch;
					}
				}
			}
			else
			{
				if (parent.childCount > 1)
				{
					for (int i = 1; i < parent.childCount; i++)
					{
						DestroyImmediate(parent.GetChild(i).gameObject);
					}
				}
			}

		});
	}
	

猜你喜欢

转载自blog.csdn.net/qq_33547099/article/details/114313114