GQ_Communicate/GQ_TongXin/Assets/Scripts/SearchName.cs

161 lines
4.3 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;
using System;
using DG.Tweening;
using Unity.VisualScripting;
public class SearchName : MonoBehaviour
{
public GameObject scrollView;
public Transform scrollViewContent;
public TMP_InputField inputField;
public GameObject prefabsText;
[Header("加入需要搜索的设备名称")]
public List<GameObject> objs = new List<GameObject>();
public List<string> objs_str = new List<string>();
//展示的名字
private List<string> SearchObjects = new List<string>();
void Start()
{
//scrollView.SetActive(false);
InputFieldEvent();
}
void InputFieldEvent()
{
inputField.onValueChanged.AddListener(a =>
{
if (a != "")
{
scrollView.SetActive(true);
Serach(a);
}
else
{
scrollView.SetActive(true);
Serach();
}
});
}
public void Serach(string strName = null)
{
SearchObjects.Clear();
for (int i = 0; i < scrollViewContent.childCount; i++)
{
//Destroy(scrollViewContent.GetChild(i).gameObject);
scrollViewContent.GetChild(i).gameObject.SetActive(false);
}
int index = 0;
foreach (var go in objs_str)
{
if (!string.IsNullOrEmpty(strName))
{
if (go.Contains(strName))
{
//SearchObjects.Add(go);
scrollViewContent.GetChild(index).gameObject.SetActive(true);
}
}
//else
//{
// SearchObjects.Add(go);
//}
index++;
}
if (string.IsNullOrEmpty(strName))
{
for (int i = 0; i < scrollViewContent.childCount; i++)
{
//Destroy(scrollViewContent.GetChild(i).gameObject);
scrollViewContent.GetChild(i).gameObject.SetActive(true);
}
}
for (int i = 0; i < SearchObjects.Count; i++)
{
GameObject tmp = Instantiate(prefabsText, scrollViewContent, false);
TextMeshProUGUI text = tmp.GetComponentInChildren<TextMeshProUGUI>();
text.text = SearchObjects[i];
}
addonClick();
}
/// <summary>
/// 机柜列表
/// </summary>
public void LoadCabinet()
{
if (!gameObject.activeSelf)
gameObject.SetActive(true);
else return;
for (int i = 0; i < scrollViewContent.childCount; i++)
{
Destroy(scrollViewContent.GetChild(i).gameObject);
}
objs.Clear();
objs_str.Clear();
SearchObjects.Clear();
for (int i = 0; i < GameManager.Inst.Cabinets_go.Count; i++)
{
objs.Add(GameManager.Inst.Cabinets_go[i]);
objs_str.Add(GameManager.Inst.Cabinets_go[i].GetComponent<DeviceQuery>().deviceList.deviceCode);
SearchObjects.Add(GameManager.Inst.Cabinets_go[i].GetComponent<DeviceQuery>().deviceList.deviceCode);
}
for (int i = 0; i < SearchObjects.Count; i++)
{
GameObject tmp = Instantiate(prefabsText, scrollViewContent, false);
TextMeshProUGUI text = tmp.GetComponentInChildren<TextMeshProUGUI>();
text.text = SearchObjects[i];
}
addonClick();
}
public void addonClick()
{
int childCount = SearchObjects.Count;
for (int i = 0; i < childCount; i++)
{
GameObject go;
try
{
go = objs[i].transform.Find(objs[i].name).gameObject;
}
catch (Exception)
{
go = objs[i];
}
scrollViewContent.GetChild(i).GetComponent<Button>().onClick.AddListener(() =>
{
if (go)
{
var c = go.GetComponent<Renderer>().materials[0].color;
GameManager.Inst.ChangeMaterialColor(go, 0, new Color(1, 109f / 255, 0, 1));
DOVirtual.Float(0, 10, 1f, null).OnComplete(() => GameManager.Inst.ChangeMaterialColor(go, 0, c));
}
});
}
}
private void Update()
{
}
}