美文网首页IT 森林UGUI专题Unity技术分享
UGUI实现模糊查询动态生成列表功能文章

UGUI实现模糊查询动态生成列表功能文章

作者: 紫龙大侠 | 来源:发表于2017-09-21 11:32 被阅读21次

这篇文章主要以案例的形式讲解如何制作一个UGUI的查询列表并且实现模糊查询和动态生成列表。

欢迎进入我的原创博客紫龙大侠技术博客

需求:场景中有一些名字不同的物体要求被查找并在列表中显示物体名字。

如下图的目录结构,一个父物体下有很多个子物体要求被查询并显示在UI的列表中。

制作过程:

1.新建一个图片用来做显示列表的窗口,也就是下图中的rect的区域。

2.在rect的图片添加如下组件:

3.然后在这个下面做一个Text子物体命名为grid,这个grid就是用来给显示的列表一个自动排版功能,grid要放到rect组件Scroll Rect的Content。如下图:

4.在grid上添加组件Grid Layout Group,然后根据列表格子的宽度和高度来决定cell size的大小。

在grid加上查找脚本Searchlogic。

如下图:

5.在gird下面新建图片命名为findnamegridcontent作为生成列表的每一个具体的列表格,并且将其做成prefab放到项目中的Resources目录下。

如下图:

Searchlogic脚本上要将查找按钮拖到指定位置,详细代码内容如下:

[csharp]view plaincopy

usingSystem.Collections;

usingSystem.Collections.Generic;

usingUnityEngine;

usingUnityEngine.UI;

publicclasssearchlogic : MonoBehaviour

{

privateGameObject gridnameshow;

publicButton searchbutton;

/// 

/// List 里存的是场景里所有的被查找物体的名称和位置

/// 

///

List allnameslist =newList();

stringinputtext ="";

GameObject searchbg;//生成的每一行的显示物体

// Use this for initialization

voidStart()

{

stringgridpath ="findnamegridcontent";//生成列表的路径

gridnameshow = Resources.Load(gridpath,typeof(GameObject))asGameObject;//加载生成的子物体

//找到场景中所有的目标物体,然后添加到list里

GameObject go = GameObject.Find("Tfather");

if(go !=null)

{

//找到场景中所有的目标物体,然后添加到list里

allnameslist =newList();

foreach(Transform childingo.transform)

{

allnameslist.Add(child);

Debug.Log(child.gameObject.name);

}

}

//初始化查找按钮

searchbutton.onClick.AddListener(Findbutton);

}

/// 

/// 查找方法触发

/// 

voidFindbutton()

{

//Grid的长度随着生成物体个数变化

this.gameObject.GetComponent().sizeDelta =newVector2(this.gameObject.GetComponent().sizeDelta.x, 0);

inputtext = GameObject.Find("searchdevice").transform.FindChild("searchtitle/InputField/Text").GetComponent().text;

// 清空grid里的所有东西

List lst =newList();

foreach(Transform childintransform)

{

lst.Add(child);

Debug.Log(child.gameObject.name);

}

for(inti = 0; i < lst.Count; i++)

{

Destroy(lst[i].gameObject);

}

compared();

}

/// 

/// 将查找文字与库里的数据对比,然后生成列表

/// 

voidcompared()

{

for(inti = 0; i < allnameslist.Count; i++)

{

Debug.Log("list 里有:"+ allnameslist[i].name);

if(inputtext !=""&& allnameslist[i].name.Contains(inputtext))

{

Debug.Log("包含"+"。。。。该字符串是:"+ allnameslist[i]);

Generatenamegrids(allnameslist[i].name);//生成列表

}

else

{

Debug.Log("不包含");

}

}

}

/// 

/// 生成整个gird子物体

/// 

voidGeneratenamegrids(stringthename)

{

//生成record的物体、

searchbg = Instantiate(gridnameshow,this.transform.position, Quaternion.identity)asGameObject;

searchbg.transform.SetParent(this.transform);

searchbg.transform.localScale =newVector3(1,1,1);

searchbg.transform.FindChild("positontext").GetComponent().text = thename;

//本grid长度加60

this.gameObject.GetComponent().sizeDelta =newVector2(this.gameObject.GetComponent().sizeDelta.x,this.gameObject.GetComponent().sizeDelta.y +this.GetComponent().cellSize.y +this.GetComponent().spacing.y);

}

}

这样在输入框中输入场景中被查找物体的关键字然后进行点击按钮就可以查找到物体了。

如下图:

相关文章

网友评论

    本文标题:UGUI实现模糊查询动态生成列表功能文章

    本文链接:https://www.haomeiwen.com/subject/rzszsxtx.html