159 lines
4.6 KiB
C#
159 lines
4.6 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text.RegularExpressions;
|
||
using TMPro;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
|
||
public class APISearchFor : MonoBehaviour
|
||
{
|
||
[SerializeField] APISon Btn;
|
||
public Dictionary<int, APIData> keyValuePairs = new Dictionary<int, APIData>();
|
||
[SerializeField] Toggle tog1;
|
||
[SerializeField] RectTransform recttrans;
|
||
[SerializeField] List<APISon> list = new List<APISon>();
|
||
[SerializeField] Transform trans;
|
||
[SerializeField] APIWindows windows;
|
||
[SerializeField] APIWindows windows2;
|
||
[SerializeField] TMP_InputField inputField;
|
||
[SerializeField] List<int> id = new List<int>();
|
||
//public List<>
|
||
// Start is called before the first frame update
|
||
void Start()
|
||
{
|
||
test();
|
||
init();
|
||
}
|
||
void init()
|
||
{
|
||
tog1.onValueChanged.AddListener((x) =>
|
||
{
|
||
if (x)
|
||
{
|
||
recttrans.sizeDelta = new Vector2(492, 480);
|
||
}
|
||
else
|
||
{
|
||
recttrans.sizeDelta = new Vector2(492, 800);
|
||
}
|
||
});
|
||
if (tog1.isOn)
|
||
{
|
||
recttrans.sizeDelta = new Vector2(492, 480);
|
||
}
|
||
else
|
||
{
|
||
recttrans.sizeDelta = new Vector2(492, 800);
|
||
}
|
||
if (windows2 == null)
|
||
{
|
||
windows2 = Instantiate(windows, transform);
|
||
}
|
||
windows2.gameObject.SetActive(false);
|
||
for (int i = 0; i < keyValuePairs.Count; i++)
|
||
{
|
||
APISon api = Instantiate(Btn, trans);
|
||
api.id = i;
|
||
api.windows2 = windows2;
|
||
api.name = keyValuePairs[i].name;
|
||
api.data = keyValuePairs[i].NameStr + i;
|
||
list.Add(api);
|
||
}
|
||
inputField.onValueChanged.AddListener((x) =>
|
||
{
|
||
//System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
|
||
//watch.Start(); //开始监视代码运行时间
|
||
FuzzyAlgorithm(x); //需要测试的代码
|
||
//tttt(x);
|
||
//watch.Stop(); //停止监视
|
||
//TimeSpan timespan = watch.Elapsed; //获取当前实例测量得出的总时间
|
||
//Debug.Log($"打开窗口代码执行时间:{timespan.TotalMilliseconds}(毫秒)"); //总毫秒数
|
||
});
|
||
}
|
||
void tttt(string x)
|
||
{
|
||
Dictionary<int, APIData> keyValuePairs = new Dictionary<int, APIData>();
|
||
if (string.IsNullOrEmpty(x))
|
||
{
|
||
return;
|
||
}
|
||
|
||
// 使用正则表达式进行模糊搜索
|
||
//List<string> result = data.Where(item => Regex.IsMatch(item, @"\w*" + x + @"\w*"))
|
||
// .ToList();
|
||
|
||
//普通字典版
|
||
//List<string> result = data.Values.Where(item => Regex.IsMatch(item, @"\w*" + keyword + @"\w*"))
|
||
// .ToList();
|
||
|
||
//字典版
|
||
List<string> result = keyValuePairs.Values.Where(item => Regex.IsMatch(item.name, @"\w*" + x + @"\w*"))
|
||
.Select(item => item.name)
|
||
.ToList();
|
||
// 输出结果
|
||
foreach (string item in result)
|
||
{
|
||
Debug.Log($"匹配到的字符串:{item}");
|
||
}
|
||
}
|
||
void test()
|
||
{
|
||
for (int i = 0; i < 50; i++)
|
||
{
|
||
APIData data = new APIData();
|
||
data.name = "设置姓名" + i;
|
||
data.NameStr = "创建机器人,方法:CREATE,参数:无参,重载:2重载,返回值:无返回值,类:SZPT";
|
||
keyValuePairs.Add(i, data);
|
||
}
|
||
}
|
||
void FuzzyAlgorithm(string x)
|
||
{
|
||
id.Clear();
|
||
if (!string.IsNullOrEmpty(x))
|
||
{
|
||
for (int i = 0; i < keyValuePairs.Count; i++)
|
||
{
|
||
if (list[i].name.Contains(x, StringComparison.Ordinal))
|
||
{
|
||
id.Add(list[i].id);
|
||
}
|
||
}
|
||
if (id.Count.Equals(0))
|
||
{
|
||
for (int i = 0; i < list.Count; i++)
|
||
{
|
||
list[i].gameObject.SetActive(false);
|
||
}
|
||
}
|
||
for (int i = 0; i < list.Count; i++)
|
||
{
|
||
for (int j = 0; j < id.Count; j++)
|
||
{
|
||
if (list[i].id.Equals(id[j]))
|
||
{
|
||
list[i].gameObject.SetActive(true);
|
||
break;
|
||
}
|
||
else
|
||
{
|
||
list[i].gameObject.SetActive(false);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
for (int i = 0; i < list.Count; i++)
|
||
{
|
||
list[i].gameObject.SetActive(true);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
public struct APIData
|
||
{
|
||
public string name;
|
||
public string NameStr;
|
||
}
|