unity 模糊查询

unity模糊查询

unity 模糊查询

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

你好! 这是你第一次使用 Markdown编辑器 所展示的欢迎页。如果你想学习如何使用Markdown编辑器, 可以仔细阅读这篇文章,了解一下Markdown的基本语法知识。

上代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class ReadText : MonoBehaviour
{
    
    
    /// <summary>
    /// 查询内容
    /// </summary>
    private InputField queries;
    /// <summary>
    /// 所有内容
    /// </summary>
    private List<string> allStr = new List<string>();
    /// <summary>
    /// 当前查询内容
    /// </summary>
    private List<string> currentStr = new List<string>();
    /// <summary>
    /// 存储目标
    /// </summary>
    private Transform OSTObj;
    /// <summary>
    /// 实例化物体
    /// </summary>
    public GameObject InstantObj;
    /// <summary>
    /// 下拉框
    /// </summary>
    private GameObject dropDownBox;
    private void Start()
    {
    
    
        queries = transform.Find("QueriesText").GetComponent<InputField>();
        queries.onValueChanged.AddListener(Changed);
        OSTObj = transform.Find("Scroll View/Viewport/Content");
        dropDownBox =transform.Find("Scroll View").gameObject;

        if (dropDownBox.activeInHierarchy)
            dropDownBox.SetActive(false);

        allStr.Add("afsdjjf");
        allStr.Add("sdfsgd");
        allStr.Add("gfhjghj");
        allStr.Add("khjgftrdf");
        allStr.Add("45fgfdg");
        allStr.Add("41fgfdg");
        allStr.Add("iuouio");
        allStr.Add("8845rs");
        allStr.Add("544fd54fds");
    }


    public void Changed(string str)
    {
    
    
        if (!dropDownBox.activeInHierarchy)
            dropDownBox.SetActive(true);
        ResetStr();
        for (int i = 0; i < allStr.Count; i++)
        {
    
    
            if (allStr[i].Contains(str))
                currentStr.Add(allStr[i]);
        }
        for (int a = 0; a < currentStr.Count; a++)
        {
    
    
            GameObject games = Instantiate(InstantObj, Vector3.zero, Quaternion.identity);
            games.name = currentStr[a];
            games.GetComponentInChildren<Text>().text = currentStr[a];
            games.transform.parent = OSTObj.transform;
            games.GetComponent<Button>().onClick.AddListener(Listenevents);
        }
    }

    public void ResetStr()
    {
    
    
        currentStr.Clear();
        if (OSTObj.childCount > 0)
        {
    
    
            for (int i = 0; i < OSTObj.childCount; i++)
                Destroy(OSTObj.GetChild(i).gameObject);
        }
    }

    public void Listenevents() 
    {
    
    
        queries.text = EventSystem.current.currentSelectedGameObject.transform.name;
        if (dropDownBox.activeInHierarchy)
            dropDownBox.SetActive(false);
    }
}

[Dome **模糊查询**](https://download.csdn.net/download/qq_19323929/12573758)

猜你喜欢

转载自blog.csdn.net/qq_19323929/article/details/107111159