unity 搜索内容

可检索中文|英文

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

public class Searchlogic : MonoBehaviour
{
    //搜索库
    private GameObject gridnameshow;
    //点击搜索按钮
    public Button searchbutton;


    /// <summary>
    /// List 里存的是场景里所有的被查找物体的名称和位置
    /// </summary>
    /// 
    List<Transform> allnameslist = new List<Transform>();
    string str_list;
    //连接inputfile中的text
    string str;
    //中间值
    string inputtext = "";
    public Text inputtext_demo;
    public Text log_show;
    GameObject searchbg;//生成的每一行的显示物体



    // Use this for initialization
    void Start()
    {
        string gridpath = "Button";//生成列表的路径
        //gridnameshow = Resources.Load(gridpath, typeof(GameObject)) as GameObject;//加载生成的子物体
        gridnameshow = Resources.Load<GameObject>(gridpath);

        //找到场景中所有的目标物体,然后添加到list里
        GameObject go = GameObject.Find("Tfather");
        if (go != null)
        {
            //找到场景中所有的目标物体,然后添加到list里
            allnameslist = new List<Transform>();

            foreach (Transform child in go.transform)
            {
                allnameslist.Add(child);
                str_list += child.name;
                Debug.Log(child.gameObject.name);
            }

        }

        //初始化查找按钮
        searchbutton.onClick.AddListener(Findbutton);
       
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Return))
        {
            Findbutton();
          
        }  
    }
    /// <summary>
    /// 查找方法触发
    /// </summary>
    void Findbutton()
    {
        log_show.text = "";
        //Grid的长度随着生成物体个数变化
        this.gameObject.GetComponent<RectTransform>().sizeDelta = new Vector2(this.gameObject.GetComponent<RectTransform>().sizeDelta.x, 0);
        //inputtext = GameObject.Find("searchdevice").transform.FindChild("InputField/Text").GetComponent<Text>().text;
         str = inputtext_demo.text;

        // 清空grid里的所有东西
        List<Transform> lst = new List<Transform>();
        foreach (Transform child in transform)
        {
            lst.Add(child);
            Debug.Log(child.gameObject.transform.GetChild(0).name);
        }
        for (int i = 0; i < lst.Count; i++)
        {
            Destroy(lst[i].gameObject);
        }

       
        inputtext=str.Replace(" ", string.Empty);
        if (inputtext != "")
        {
            compared();
        }
        
        
    }

    /// <summary>
    /// 将查找文字与库里的数据对比,然后生成列表
    /// </summary>
    void compared()
    {
        checkString(inputtext);
        for (int i = 0; i < allnameslist.Count; i++)
        {
            Debug.Log("list 里有:" + allnameslist[i].name); 
        }

    }
    //中文判断
    // 判断 当前字符是否为中文
    public bool isChinese(char c)
    {
        return c >= 0x4E00 && c <= 0x9FA5;
    }

    public void checkString(string str)
    {
        char[] ch = str.ToCharArray();
        print(ch.Length);
        if (str != null)
        {
            for (int i = 0; i < ch.Length; i++)
            {
                if (isChinese(ch[i]))
                {
                    

                    if (!str_list.Contains(ch[i].ToString()))
                    {
                        log_show.text = "未检索到相关中文!";
                    }
                    else
                    {
                       
                        for (int j = 0; j < allnameslist.Count; j++)
                        {

                            if (allnameslist[j].name.Contains(ch[i].ToString()))
                            {
                                Generatenamegrids(allnameslist[j].name);//生成列表
                            }

                        }
                    }
                   

                }
                else
                {
                    string str_low = ch[i].ToString().ToLower();

                    string str_up = ch[i].ToString().ToUpper();
                    
                    if (!str_list.Contains(str_low)&& !str_list.Contains(str_up))
                    {
                        log_show.text = "未检索到相关英文!";
                    }
                    else
                    {
                        for (int j = 0; j < allnameslist.Count; j++)
                        {
                            if (allnameslist[j].name.Contains(str_low) || allnameslist[j].name.Contains(str_up))
                            {
                                Generatenamegrids(allnameslist[j].name);
                            }

                        }
                    }
                    

                }
            }
        }

    }

    /// <summary>
    /// 生成整个gird子物体
    /// </summary>
    void Generatenamegrids(string thename)
    {

        //生成record的物体、
        searchbg =GameObject.Instantiate(gridnameshow, this.transform.position, Quaternion.identity) as GameObject;
        searchbg.transform.SetParent(this.transform);
        searchbg.transform.localScale = new Vector3(1, 1, 1);
        searchbg.transform.FindChild("Text").GetComponent<Text>().text = thename;

        //本grid长度加
        //this.gameObject.GetComponent<RectTransform>().sizeDelta = new Vector2(this.gameObject.GetComponent<RectTransform>().sizeDelta.x, this.gameObject.GetComponent<RectTransform>().sizeDelta.y + this.GetComponent<GridLayoutGroup>().cellSize.y + this.GetComponent<GridLayoutGroup>().spacing.y);
    }

    

}

 

  • 3
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 12
    评论
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值