GQ_Communicate/GQ_TongXin/Assets/script/SearchName1.cs

98 lines
2.6 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 Button prefabsText;
/// <summary>
/// 搜索按钮
/// </summary>
public Button serarchBtn;
public DragController dragController;
[Header("加入需要搜索的设备名称")]
public List<GameObject> objs = new List<GameObject>();
public List<GameObject> SearchObjects = new List<GameObject>();
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<TextMeshProUGUI>();
text.text = SearchObjects[index].name;
tmp.onClick.AddListener(() =>
{
dragController.oriObjectPrefab = SearchObjects[index].GetComponent<DragTest1>();
dragController.gameObject.SetActive(true);
dragController._icon.sprite = SearchObjects[index].GetComponent<DragTest1>().selfIcon;
dragController._icon.SetNativeSize();
});
}
}
//TODO:有问题
void OnClickSearchBtn()
{
serarchBtn.onClick.AddListener(() =>
{
if (searchText.text != "")
{
scrollView.SetActive(true);
Serach(searchText.text.Trim());
}
else scrollView.SetActive(false);
});
}
}