Unity 简单搜索功能
public InputField inputField;
public RectTransform parent;
public RectTransform item;
public string[] content;
void Start()
{
inputField.onValueChanged.AddListener((value) =>
{
if (parent.childCount > 1)
{
for (int i = 1; i < parent.childCount; i++)
{
DestroyImmediate(parent.GetChild(i).gameObject);
}
}
Debug.Log("value==" + value);
if (!string.IsNullOrEmpty(value))
{
if (parent.childCount > 1)
{
for (int i = 1; i < parent.childCount; i++)
{
DestroyImmediate(parent.GetChild(i).gameObject);
}
}
foreach (var ch in content)
{
//关键比对代码
if (ch.IndexOf(value, StringComparison.OrdinalIgnoreCase) >= 0)
{
RectTransform rt = Instantiate(item, parent);
rt.GetChild(0).GetComponent<Text>().text = ch;
}
}
}
else
{
if (parent.childCount > 1)
{
for (int i = 1; i < parent.childCount; i++)
{
DestroyImmediate(parent.GetChild(i).gameObject);
}
}
}
});
}