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; /// /// 搜索文本框预制体 /// public Button prefabsText; /// /// 搜索按钮 /// public Button serarchBtn; public DragController dragController; [Header("加入需要搜索的设备名称")] public List objs = new List(); public List SearchObjects = new List(); void Start() { //scrollView.SetActive(false); InputFieldEvent(); dragController.gameObject.SetActive(false); //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++) { int index = i; Button tmp = Instantiate(prefabsText, scrollViewContent, false); TextMeshProUGUI text = tmp.transform.GetChild(0).GetComponent(); text.text = SearchObjects[index].name; tmp.onClick.AddListener(() => { dragController.oriObjectPrefab = SearchObjects[index].GetComponent(); dragController.gameObject.SetActive(true); dragController._icon.sprite = SearchObjects[index].GetComponent().selfIcon; dragController._icon.SetNativeSize(); }); } } //TODO:有问题 void OnClickSearchBtn() { serarchBtn.onClick.AddListener(() => { if (searchText.text != "") { scrollView.SetActive(true); Serach(searchText.text.Trim()); } else scrollView.SetActive(false); }); } }