GQ_Communicate/GQ_TongXin/Assets/script/SearchName1.cs

67 lines
1.6 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UIElements;
using UnityEngine.UI;
public class SearchName1 : MonoBehaviour
{
public GameObject scrollView;
public Transform scrollViewContent;
public TMP_InputField inputField;
public GameObject prefabsText;
[Header("加入需要搜索的设备名称")]
public List<GameObject> objs = new List<GameObject>();
private List<GameObject> SearchObjects = new List<GameObject>();
void Start()
{
scrollView.SetActive(false);
InputFieldEvent();
}
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.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;
}
}
}