欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Unity实现首字母检索器

程序员文章站 2022-06-24 11:57:24
本文实例为大家分享了unity实现首字母检索器的具体代码,供大家参考,具体内容如下需要实现一个类似 “城市选择器”的功能 网上基本都是用原生或者前端来实现功能 其他大概的思路都差不多 这里提供一个un...

本文实例为大家分享了unity实现首字母检索器的具体代码,供大家参考,具体内容如下

需要实现一个类似 “城市选择器”的功能 网上基本都是用原生或者前端来实现功能 其他大概的思路都差不多 这里提供一个unity 实现的思路

先看一下效果

Unity实现首字母检索器

这里用到了 superscrollview 这个插件 来实现功能

listtext.cs  // 核心控制类  代码写的比较随意 大概的功能已经实现,需要根据实际需要进行优化。

using superscrollview;
using system.collections;
using system.collections.generic;
using unityengine;
using unityengine.ui;
 
public class listtext : monobehaviour
{
 public looplistview2 mlooplistview;
 public text select_text;
 public recttransform right_content;
 public gameobject tag_prefab;
 public string[] tags = new string[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "#" };
 
 public int treeviewitemcount
 {
 get
 {
 return mitemdatalist.count;
 }
 }
 // start is called before the first frame update
 void start()
 {
 init();
 
 keys.clear();
 for (int i = 0; i < tags.length; ++i)
 {
 citydata citydata = new citydata();
 citydata.key = tags[i];
 list<string> value = new list<string>();
 value.add("item" + tags[i] + random.range(0, 6).tostring());
 citydata.value = value;
 
 keys.add(citydata);
 }
 dorefreshdatasource();
 int count = treeviewitemcount;
 //tells mtreeitemcountmgr there are how many treeitems and every treeitem has how many childitems.
 for (int i = 0; i < count; ++i)
 {
 int childcount = getitemdatabyindex(i).childcount;
 //second param "true" tells mtreeitemcountmgr this treeitem is in expand status, that is to say all its children are showing.
 addtreeitem(childcount, true);
 }
 
 mlooplistview.initlistview(gettotalitemandchildcount(), ongetitembyindex);
 }
 
 public void addtreeitem(int count, bool isexpand)
 {
 keydata data = new keydata();
 data.mtreeitemindex = mtreeitemdatalist.count;
 data.mchildcount = count;
 data.misexpand = isexpand;
 mtreeitemdatalist.add(data);
 misdirty = true;
 }
 public int gettotalitemandchildcount()
 {
 int count = mtreeitemdatalist.count;
 if (count == 0)
 {
 return 0;
 }
 updatealltreeitemdataindex();
 return mtreeitemdatalist[count - 1].mendindex + 1;
 }
 looplistviewitem2 ongetitembyindex(looplistview2 listview, int index)
 {
 if (index < 0)
 {
 return null;
 }
 
 keydata countdata = querytreeitembytotalindex(index);
 if (countdata == null)
 {
 return null;
 }
 int treeitemindex = countdata.mtreeitemindex;
 valuedata treeviewitemdata = getitemdatabyindex(treeitemindex);
 if (countdata.ischild(index) == false)// if is a treeitem
 {
 //get a new treeitem
 looplistviewitem2 item = listview.newlistviewitem("keyitem");
 keyitem itemscript = item.getcomponent<keyitem>();
 if (item.isinithandlercalled == false)
 {
 item.isinithandlercalled = true;
 itemscript.init();
 //itemscript.setclickcallback(this.onexpandclicked);
 }
 //update the treeitem's content
 itemscript.mtext.text = treeviewitemdata.mname;
 itemscript.setitemdata(treeitemindex, countdata.misexpand);
 return item;
 }
 else // if is a treechilditem
 {
 //childindex is from 0 to childcount.
 //for example, treechilditem0_0 is the 0'th child of treeitem0
 //and treechilditem1_2 is the 2'th child of treeitem1
 int childindex = countdata.getchildindex(index);
 itemdata itemdata = treeviewitemdata.getchild(childindex);
 if (itemdata == null)
 {
 return null;
 }
 //get a new treechilditem
 looplistviewitem2 item = listview.newlistviewitem("valueitem");
 valueitem itemscript = item.getcomponent<valueitem>();
 if (item.isinithandlercalled == false)
 {
 item.isinithandlercalled = true;
 itemscript.init();
 }
 //update the treechilditem's content
 itemscript.setitemdata(itemdata, treeitemindex, childindex);
 return item;
 }
 
 }
 
 list<valuedata> mitemdatalist = new list<valuedata>();
 
 int mtreeviewitemcount = 20;
 int mtreeviewchilditemcount = 30;
 
 // list<string, list<string>> keys = new list<string, list<string>>();
 
 
 arraylist keys = new arraylist();
 
 void dorefreshdatasource()
 {
 mitemdatalist.clear();
 
 
 for (int i = 0; i < keys.count; i++)
 {
 valuedata tdata = new valuedata();
 citydata city = keys[i] as citydata;
 tdata.mname = "" + city.key;
 mitemdatalist.add(tdata);
 // int childcount = random.range(0, 6);
 for (int j = 0; j < city.value.count; j++)
 {
 itemdata childitemdata = new itemdata();
 childitemdata.mname = "item" + city.value[j] + ":child" + j;
 childitemdata.mdesc = "item desc for " + childitemdata.mname;
 childitemdata.mstarcount = random.range(0, 6);
 childitemdata.mfilesize = random.range(20, 999);
 tdata.addchild(childitemdata);
 }
 }
 //for (int i = 0; i < keys.count; ++i)
 //{
 // valuedata tdata = new valuedata();
 // tdata.mname = "" + keys[]
 // mitemdatalist.add(tdata);
 // int childcount = random.range(0, 6);
 
 //}
 }
 
 public void additem()
 {
 // string key = tags[random.range(0, tags.length)];
 // int itemindex = random.range(0, tags.length);
 int itemindex = 0;
 citydata citydata = keys[itemindex] as citydata;
 
 citydata.value.add(citydata.key + "测试");
 
 keys[itemindex] = citydata;
 
 // int itemindex = 0;
 int childindex = 0;
 
 if (childindex < 0)
 {
 childindex = 0;
 }
 keydata itemcountdata = gettreeitem(itemindex);
 if (itemcountdata == null)
 {
 return;
 }
 addnewitemchildfortest(itemindex, childindex);
 int childcount = itemcountdata.mchildcount;
 setitemchildcount(itemindex, childcount + 1);
 dorefreshdatasource();
 mlooplistview.setlistitemcount(gettotalitemandchildcount(), false);
 
 mlooplistview.refreshallshownitem();
 }
 public void setitemchildcount(int treeindex, int count)
 {
 if (treeindex < 0 || treeindex >= mtreeitemdatalist.count)
 {
 return;
 }
 misdirty = true;
 keydata data = mtreeitemdatalist[treeindex];
 data.mchildcount = count;
 }
 public void addnewitemchildfortest(int itemindex, int addtobeforechildindex)
 {
 if (itemindex < 0 || itemindex >= mitemdatalist.count)
 {
 return;
 }
 valuedata tdata = mitemdatalist[itemindex];
 list<itemdata> childitemdatalist = tdata.mchilditemdatalist;
 itemdata childitemdata = new itemdata();
 childitemdata.mname = "item" + itemindex + ":" + addtobeforechildindex;
 childitemdata.mdesc = "item desc for " + childitemdata.mname;
 childitemdata.mstarcount = random.range(0, 6);
 childitemdata.mfilesize = random.range(20, 999);
 if (addtobeforechildindex < 0)
 {
 childitemdatalist.insert(0, childitemdata);
 }
 else if (addtobeforechildindex >= childitemdatalist.count)
 {
 childitemdatalist.add(childitemdata);
 }
 else
 {
 childitemdatalist.insert(addtobeforechildindex, childitemdata);
 }
 
 }
 
 public valuedata getitemdatabyindex(int index)
 {
 if (index < 0 || index >= mitemdatalist.count)
 {
 return null;
 }
 return mitemdatalist[index];
 }
 
 list<keydata> mtreeitemdatalist = new list<keydata>();
 keydata mlastqueryresult = null;
 bool misdirty = true;
 public keydata querytreeitembytotalindex(int totalindex)
 {
 if (totalindex < 0)
 {
 return null;
 }
 int count = mtreeitemdatalist.count;
 if (count == 0)
 {
 return null;
 }
 updatealltreeitemdataindex();
 if (mlastqueryresult != null)
 {
 if (mlastqueryresult.mbeginindex <= totalindex && mlastqueryresult.mendindex >= totalindex)
 {
 return mlastqueryresult;
 }
 }
 int low = 0;
 int high = count - 1;
 keydata data = null;
 while (low <= high)
 {
 int mid = (low + high) / 2;
 data = mtreeitemdatalist[mid];
 if (data.mbeginindex <= totalindex && data.mendindex >= totalindex)
 {
 mlastqueryresult = data;
 return data;
 }
 else if (totalindex > data.mendindex)
 {
 low = mid + 1;
 }
 else
 {
 high = mid - 1;
 }
 }
 return null;
 }
 void updatealltreeitemdataindex()
 {
 if (misdirty == false)
 {
 return;
 }
 mlastqueryresult = null;
 misdirty = false;
 int count = mtreeitemdatalist.count;
 if (count == 0)
 {
 return;
 }
 keydata data0 = mtreeitemdatalist[0];
 data0.mbeginindex = 0;
 data0.mendindex = (data0.misexpand ? data0.mchildcount : 0);
 int curend = data0.mendindex;
 for (int i = 1; i < count; ++i)
 {
 keydata data = mtreeitemdatalist[i];
 data.mbeginindex = curend + 1;
 data.mendindex = data.mbeginindex + (data.misexpand ? data.mchildcount : 0);
 curend = data.mendindex;
 }
 }
 public keydata gettreeitem(int treeindex)
 {
 if (treeindex < 0 || treeindex >= mtreeitemdatalist.count)
 {
 return null;
 }
 return mtreeitemdatalist[treeindex];
 }
 public void onjumpbtnclicked(int itemindex)
 {
 // int itemindex = 0;
 int childindex = 0;
 int finalindex = 0;
 if (childindex < 0)
 {
 childindex = 0;
 }
 keydata itemcountdata = gettreeitem(itemindex);
 if (itemcountdata == null)
 {
 return;
 }
 int childcount = itemcountdata.mchildcount;
 if (itemcountdata.misexpand == false || childcount == 0 || childindex == 0)
 {
 finalindex = itemcountdata.mbeginindex;
 }
 else
 {
 if (childindex > childcount)
 {
 childindex = childcount;
 }
 if (childindex < 1)
 {
 childindex = 1;
 }
 finalindex = itemcountdata.mbeginindex + childindex;
 }
 mlooplistview.movepaneltoitemindex(finalindex, 0);
 }
 
 
 public void init()
 {
 int index = 0;
 foreach (string value in tags)
 {
 gameobject go = instantiate(tag_prefab, right_content);
 go.name = "tag-" + value;
 tag_item tag_item = go.getcomponent<tag_item>();
 tag_item.select_text = select_text;
 tag_item.init(value);
 tag_item.index = index;
 tag_item.keystr = value;
 tag_item.listtext = this;
 index += 1;
 }
 }
 
 
}
 
public class citydata
{
 public string key;
 public list<string> value;
}

这里提供 源码下载  unity版本2019.4.6 低版本也可以打开

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。