GQ_Communicate/GQ_TongXin/Assets/Scripts/SearchName.cs

263 lines
7.6 KiB
C#

using DG.Tweening;
using System;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 搜索框
/// </summary>
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>();
public SearchType searchType;
void Start()
{
searchType = SearchType.None;
//scrollView.SetActive(false);
InputFieldEvent();
}
void InputFieldEvent()
{
inputField.onValueChanged.AddListener(a =>
{
if (searchType == SearchType.None)
return;
if (a != "")
{
scrollView.SetActive(true);
Serach(a);
}
else
{
scrollView.SetActive(true);
Serach(null);
}
});
}
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(SearchType type)
{
searchType = type;
if (!gameObject.activeSelf)
gameObject.SetActive(true);
else return;
init();
for (int i = 0; i < GameManager.Inst.Cabinets_go.Count; i++)
{
objs.Add(GameManager.Inst.Cabinets_go[i]);
string s = GameManager.Inst.Cabinets_go[i].GetComponent<DeviceQuery>().deviceList.deviceCode;
objs_str.Add(s);
SearchObjects.Add(s);
}
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 addonClick()
{
int childCount = SearchObjects.Count;
for (int i = 0; i < childCount; i++)
{
scrollViewContent.GetChild(i).GetComponent<Button>().onClick.RemoveAllListeners();
FindGo(objs, i, (go) =>
{
switch (searchType)
{
case SearchType.None:
break;
case SearchType.:
{
scrollViewContent.GetChild(i).GetComponent<Button>().onClick.AddListener(() =>
{
if (go)
{
var c = go.GetComponent<Renderer>().materials[0].color;
Debug.Log(1 + "---------------------- c1");
GameManager.Inst.ChangeMaterialColor(go, 0, new Color(1, 109f / 255, 0, 1));
Debug.Log(2 + "---------------------- c2");
DOVirtual.Float(0, 10, 1f, null).OnComplete(() => GameManager.Inst.ChangeMaterialColor(go, 0, c));
Debug.Log(3 + "---------------------- c3");
}
});
}
break;
case SearchType.线:
{
scrollViewContent.GetChild(i).GetComponent<Button>().onClick.AddListener(() =>
{
if (go)
{
var c = go.GetComponent<TransparentGlow>();
Debug.Log(1 + "---------------------- f1");
c.F2();
Debug.Log(2 + "---------------------- f2");
DOVirtual.Float(0, 10, 1f, null).OnComplete(() => c.F1());
Debug.Log(3 + "---------------------- f3");
}
});
}
break;
default:
break;
}
});
}
}
// 寻找机柜
void FindGo(List<GameObject> objs, int index,Action<GameObject> callback)
{
GameObject go = null;
try
{
go = objs[index].transform.Find(objs[index].name).gameObject;
}
catch
{
go = objs[index];
}
callback?.Invoke(go);
}
public void LoadXianLan(SearchType type)
{
searchType = type;
if (!gameObject.activeSelf)
gameObject.SetActive(true);
else return;
init();
CreateLine createLine = PatternChoose.Inst.transform.Find("画线").GetComponent<CreateLine>();
for (int i = 0; i < createLine.list7.Count; i++)
{
objs.Add(createLine.list7[i].gameObject);
if (i != 0 && i % 2 != 0)
{
string s = createLine.list7[i].GetComponent<PortQuery>().portList.remark;
objs_str.Add(s);
SearchObjects.Add(s);
}
}
for (int i = 0; i < SearchObjects.Count; i++)
{
GameObject tmp = Instantiate(prefabsText, scrollViewContent, false);
TextMeshProUGUI text = tmp.GetComponentInChildren<TextMeshProUGUI>();
text.text = SearchObjects[i];
}
addonClick();
}
void init()
{
for (int i = 0; i < scrollViewContent.childCount; i++)
{
Destroy(scrollViewContent.GetChild(i).gameObject);
}
objs.Clear();
objs_str.Clear();
SearchObjects.Clear();
inputField.transform.Find("Text Area/Placeholder").GetComponent<TextMeshProUGUI>().text = "请输入要搜索的" + searchType.ToString();
}
private void Update()
{
}
/// <summary>
/// 搜索框类型
/// </summary>
public enum SearchType
{
None,
,
线
}
}