GQ_Communicate/GQ_TongXin/Assets/script/SearchName1.cs

88 lines
2.1 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;
public class SearchName1 : MonoBehaviour
{
public GameObject scrollView;
public Transform scrollViewContent;
public TMP_InputField inputField;
public TextMeshProUGUI searchText;
/// <summary>
/// 搜索文本框预制体
/// </summary>
public GameObject prefabsText;
/// <summary>
/// 搜索按钮
/// </summary>
public Button serarchBtn;
[Header("加入需要搜索的设备名称")]
public List<GameObject> objs = new List<GameObject>();
private List<GameObject> SearchObjects = new List<GameObject>();
void Start()
{
//scrollView.SetActive(false);
InputFieldEvent();
//OnClickSearchBtn();
}
void InputFieldEvent()
{
inputField.onValueChanged.AddListener(a =>
{
if (a != "")
{
//scrollView.SetActive(true);
Serach(a);
}
//else scrollView.SetActive(false);
});
}
void Serach(string strName)
{
SearchObjects.Clear();
for (int i = 0; i < scrollViewContent.childCount; i++)
{
Destroy(scrollViewContent.GetChild(i).gameObject);
}
foreach (GameObject go in objs)
{
if ((go.name).ToString().Contains(strName))
{
SearchObjects.Add(go);
}
}
for (int i = 0; i < SearchObjects.Count; i++)
{
GameObject tmp= Instantiate(prefabsText, scrollViewContent,false);
TextMeshProUGUI text= tmp.transform.GetChild(0).GetComponent<TextMeshProUGUI>();
text.text = SearchObjects[i].name;
}
}
//TODO:有问题
void OnClickSearchBtn()
{
serarchBtn.onClick.AddListener(() =>
{
if (searchText.text != "")
{
scrollView.SetActive(true);
Serach(searchText.text.Trim());
}
else scrollView.SetActive(false);
});
}
}