GQ_Communicate/GQ_TongXin/Assets/Scripts/SearchName.cs

321 lines
9.7 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);
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)
{
TransparentGlowManage.Inst.transparencyALL(GameObject.Find("机柜").GetComponentsInChildren<TransparentGlow>());
CreateLine createLine = PatternChoose.Inst.transform.Find("画线").GetComponent<CreateLine>();
var lines = createLine.xianLan.Find(go.GetComponent<PortQuery>().portList.remark).GetComponent<LineInfor>().lines;
var A = GameManager.Inst.FindParent(lines[0].gameObject, GameManager.Inst.IsDesiredParent);
var B = GameManager.Inst.FindParent(lines[1].gameObject, GameManager.Inst.IsDesiredParent);
List<TransparentGlow> transparentGlows = new List<TransparentGlow>();
transparentGlows.Add(A.GetComponent<TransparentGlow>());
transparentGlows.Add(B.GetComponent<TransparentGlow>());
TransparentGlowManage.Inst.renewALL(transparentGlows.ToArray());
}
});
}
break;
default:
break;
}
});
}
}
Predicate<Transform> findCabinet = (Transform go) =>
{
//端口-设备 go.parent
//端口-板卡-设备 go.parent.parent
var p = go.parent;
if (p.parent.parent.parent.parent.GetComponent<DeviceQuery>())
{
if (p.parent.parent.parent.parent.GetComponent<DeviceQuery>().deviceList.type == "1")
{
return true;
}
else if (p.parent.parent.parent.GetComponent<DeviceQuery>())
{
if (p.parent.parent.parent.GetComponent<DeviceQuery>().deviceList.type == "1")
{
return true;
}
}
}
else if (p.parent.parent.parent.GetComponent<DeviceQuery>())
{
if (p.parent.parent.parent.GetComponent<DeviceQuery>().deviceList.type == "1")
{
return true;
}
}
return false;
};
private bool Isitive(Transform obj)
{
throw new NotImplementedException();
}
// 寻找机柜
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);
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];
}
TransparentGlowManage.Inst.renewALL(GameObject.Find("机柜").GetComponentsInChildren<TransparentGlow>());
//List<TransparentGlow> transparentGlows = new List<TransparentGlow>();
//for (int i = 0; i < createLine.list7.Count; i++)
//{
// transparentGlows.Add(GameManager.Inst.FindParent(createLine.list7[i].gameObject, GameManager.Inst.IsDesiredParent).GetComponent<TransparentGlow>());
//}
//TransparentGlowManage.Inst.renewALL(transparentGlows.ToArray());
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().Split('_')[0];
}
private void Update()
{
}
/// <summary>
/// 搜索框类型
/// </summary>
public enum SearchType
{
None,
,
线_展示
}
}