Tz2/Assets/Zion/Scripts/Excel/ExcelSearching.cs

162 lines
5.0 KiB
C#

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<InputField> targetList = new List<InputField>();
private List<float> h_values = new List<float>();
private List<float> v_values = new List<float>();
// Start is called before the first frame update
void Start()
{
searchBtn.onClick.AddListener(delegate () { Search(searchField.text); });//搜索按钮点击事件监听
Invoke("Adddata", 2);
}
/// <summary>
/// 搜索菜单树
/// </summary>
/// <param name="str"></param>
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<RectTransform>().sizeDelta = targetList[index].transform.GetComponent<RectTransform>().sizeDelta;
temp.transform.SetSiblingIndex(0);
index++;
}
else
{
index = 0;
targetList.Clear();
h_values.Clear();
v_values.Clear();
}
}
private void Adddata()
{
contentArray = inputParent.GetComponentsInChildren<InputField>();
Debug.Log(contentArray.Length);
}
}