using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class ExcelSearching : MonoBehaviour { public Button searchBtn; public InputField searchField; public Transform inputParent; public Scrollbar scrollbar_h; public Scrollbar scrollbar_v; public GameObject hilightImage; private string oldStr = string.Empty; private GameObject tempHighlightImage; private InputField[] contentArray; private bool isHaveResult = false; private int index = 0; float h; float v; private List targetList = new List(); private List h_values = new List(); private List v_values = new List(); // Start is called before the first frame update void Start() { searchBtn.onClick.AddListener(delegate () { Search(searchField.text); });//搜索按钮点击事件监听 Invoke("Adddata", 2); } /// /// 搜索菜单树 /// /// public void Search(string str) { if (string.IsNullOrEmpty(str)) return; if (oldStr.Equals(str)) { if (targetList.Count > 0) // 开始执行搜索到的 { ShowSearchingResult(); } else { isHaveResult = false; foreach (var item in contentArray) { if (item.text.Contains(str)) { isHaveResult = true; targetList.Add(item); h = item.transform.GetSiblingIndex() * 1.0f / item.transform.parent.childCount; h_values.Add(h); v = item.transform.parent.GetSiblingIndex() * 1.0f / item.transform.parent.parent.childCount; v_values.Add(v); } } if (isHaveResult) { ShowSearchingResult(); } else { Debug.Log(string.Format("未查询到有关\"{0}\"的节点,请重新输入", str)); } } } else { index = 0; targetList.Clear(); h_values.Clear(); v_values.Clear(); if (tempHighlightImage) { DestroyImmediate(tempHighlightImage); } isHaveResult = false; foreach (var item in contentArray) { if (item.text.Contains(str)) { isHaveResult = true; targetList.Add(item); h = item.transform.GetSiblingIndex() * 1.0f / item.transform.parent.childCount; h_values.Add(h); v = item.transform.parent.GetSiblingIndex() * 1.0f / item.transform.parent.parent.childCount; v_values.Add(v); } } if (isHaveResult) { ShowSearchingResult(); } else { Debug.Log(string.Format("未查询到有关\"{0}\"的节点,请重新输入", str)); } oldStr = str; } //StartCoroutine(ReDisplay(true, inputFieldWarning.gameObject)); //inputFieldWarning.text = string.Format("未查询到有关\"{0}\"的节点!\n请重新输入", CalculatorStrLenth(str, 7)); //Debug.Log(string.Format("未查询到有关\"{0}\"的节点,请重新输入", str)); //searchField.text = ""; } private void ShowSearchingResult() { if (index <= targetList.Count - 1) { if (scrollbar_h.gameObject.activeInHierarchy) { scrollbar_h.value = h_values[index]; } if (scrollbar_v.gameObject.activeInHierarchy) { scrollbar_v.value = v_values[index]; } if (tempHighlightImage) { DestroyImmediate(tempHighlightImage); } GameObject temp = Instantiate(hilightImage) as GameObject; temp.SetActive(true); tempHighlightImage = temp; temp.transform.parent = targetList[index].transform; temp.transform.localPosition = Vector3.zero; temp.transform.localScale = Vector3.one; temp.GetComponent().sizeDelta = targetList[index].transform.GetComponent().sizeDelta; temp.transform.SetSiblingIndex(0); index++; } else { index = 0; targetList.Clear(); h_values.Clear(); v_values.Clear(); } } private void Adddata() { contentArray = inputParent.GetComponentsInChildren(); Debug.Log(contentArray.Length); } }