1091201 建立搜尋器
by 黃國哲 2020-12-01 21:18:56, 回應(0), 人氣(474)
using System;
using UnityEngine;
namespace LastZero.Utility
{
public static class SearchController {
/// <summary>
/// 搜索
/// </summary>
/// <param name="parent">父物体</param>
/// <param name="key">关键字</param>
public static void Search(Transform parent, string key)
{
for (int i = 0; i < parent.childCount; i++)
{
if (parent.GetChild(i).name.Contains(key,StringComparison.OrdinalIgnoreCase))
{
parent.GetChild(i).gameObject.SetActive(true);
}
else
{
parent.GetChild(i).gameObject.SetActive(false);
}
}
}
/// <summary>
/// 还原
/// </summary>
/// <param name="parent">父物体</param>
public static void Reset(Transform parent)
{
for (int i = 0; i < parent.childCount; i++)
{
parent.GetChild(i).gameObject.SetActive(true);
}
}
/// <summary>
/// 判断是否包含某字符串(不区分大小写)
/// </summary>
/// <param name="input">被查找字符串</param>
/// <param name="strChecked">检查是否含有的字符串</param>
/// <returns>bool</returns>
public static bool Contains(this string input, string strChecked, StringComparison stringComparison)
{
return input.IndexOf(strChecked, stringComparison) >= 0;//StringComparison枚举定义一些规则,OrdinalIgnoreCase为忽略大小写
}
}
}
//////////////////////////
using System;
using System.Globalization;
using System.Threading;
public class Example
{
public static void Main()
{
String[] cultureNames = { "en-US", "se-SE" };
String[] strings1 = { "case", "encyclopædia",
"encyclopædia", "Archæology" };
String[] strings2 = { "Case", "encyclopaedia",
"encyclopedia" , "ARCHÆOLOGY" };
StringComparison[] comparisons = (StringComparison[]) Enum.GetValues(typeof(StringComparison));
foreach (var cultureName in cultureNames) {
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cultureName);
Console.WriteLine("Current Culture: {0}", CultureInfo.CurrentCulture.Name);
for (int ctr = 0; ctr <= strings1.GetUpperBound(0); ctr++) {
foreach (var comparison in comparisons)
Console.WriteLine(" {0} = {1} ({2}): {3}", strings1[ctr],
strings2[ctr], comparison,
String.Equals(strings1[ctr], strings2[ctr], comparison));
Console.WriteLine();
}
Console.WriteLine();
}
}
}
////////////////////////////////////////
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Linq;
public class Test1: MonoBehaviour {
public InputField inputText;//该输入框放置在DropDown处
public Dropdown dropDownList;
List<string> strList;
List<Dropdown.OptionData> options = new List<Dropdown.OptionData>();//这是用来存Dropdown.OptionData的中间变量
// Use this for initialization
void Start () {
//strList内的内容设置好后就不要再进行改动了
strList = new List<string>();
strList.Add("北京");
strList.Add("上海");
strList.Add("深圳");
strList.Add("广州");
strList.Add("内蒙古");
strList.Add("蒙古大草原");
options.Add(new Dropdown.OptionData("请选择所在地区"));
foreach (string s in strList)
{
options.Add(new Dropdown.OptionData(s));
}
dropDownList.GetComponent<Dropdown>().options = options;
}
void Update()
{
}
//这个函数放在inputFeild的OnEndEdit事件下
public void onValueChange()
{
//使用LINQ进行查询
var results = from temp in strList where (temp.Contains(inputText.text)) select temp;
options.Clear();
foreach (string s in results)
{
//Debug.Log(s);
options.Add(new Dropdown.OptionData(s));
dropDownList.GetComponent<Dropdown>().options = options;
}
dropDownList.Show();
}
//这个函数放在DropDown的OnValueChange事件下
public void DropDownOnValueChange()
{
inputText.text = dropDownList.transform.GetChild(0).GetComponent<Text>().text;
options.Clear();
foreach (string s in strList)
{
options.Add(new Dropdown.OptionData(s));
}
dropDownList.GetComponent<Dropdown>().options = options;
}
}
回應