unity 搜索内容

可检索中文|英文

unity 搜索内容_第1张图片unity 搜索内容_第2张图片

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

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


    /// 
    /// List 里存的是场景里所有的被查找物体的名称和位置
    /// 
    /// 
    List allnameslist = new List();
    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(gridpath);

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

            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();
          
        }  
    }
    /// 
    /// 查找方法触发
    /// 
    void Findbutton()
    {
        log_show.text = "";
        //Grid的长度随着生成物体个数变化
        this.gameObject.GetComponent().sizeDelta = new Vector2(this.gameObject.GetComponent().sizeDelta.x, 0);
        //inputtext = GameObject.Find("searchdevice").transform.FindChild("InputField/Text").GetComponent().text;
         str = inputtext_demo.text;

        // 清空grid里的所有东西
        List lst = new List();
        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();
        }
        
        
    }

    /// 
    /// 将查找文字与库里的数据对比,然后生成列表
    /// 
    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);
                            }

                        }
                    }
                    

                }
            }
        }

    }

    /// 
    /// 生成整个gird子物体
    /// 
    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 = thename;

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

    

}

 

你可能感兴趣的:(unity,脚本,c#)