服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - Unity实现首字母检索器

Unity实现首字母检索器

2022-10-19 12:51依旧im C#

这篇文章主要为大家详细介绍了Unity实现首字母检索器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

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

先看一下效果

Unity实现首字母检索器

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
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 低版本也可以打开

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

原文链接:https://blog.csdn.net/a1228267639/article/details/109627839

延伸 · 阅读

精彩推荐
  • C#浅谈C#中对引用类型的误解

    浅谈C#中对引用类型的误解

    这篇文章主要介绍了浅谈C#中对引用类型的误解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面...

    qylost8582022-08-01
  • C#WPF实现图片合成或加水印的方法【2种方法】

    WPF实现图片合成或加水印的方法【2种方法】

    这篇文章主要介绍了WPF实现图片合成或加水印的方法,结合实例形式分析了2种比较实用的WPF图片操作相关技巧,需要的朋友可以参考下...

    Rising_Sun4252021-12-29
  • C#C#操作图片读取和存储SQLserver实现代码

    C#操作图片读取和存储SQLserver实现代码

    用C#将Image转换成byte[]并插入数据库/将图片数据从SQLserver中取出来并显示到pictureBox控件上,接下来将为你详细介绍下实现步骤,感兴趣的你可以参考下...

    C#教程网2892020-12-18
  • C#详解C#编程中异常的创建和引发以及异常处理

    详解C#编程中异常的创建和引发以及异常处理

    这篇文章主要介绍了C#编程中异常的创建和引发以及异常处理,文中介绍了Catch块和Finally块等基本的异常处理要点,需要的朋友可以参考下...

    C#教程网9252021-11-11
  • C#轻松学习C#的foreach迭代语句

    轻松学习C#的foreach迭代语句

    轻松学习C#的foreach迭代语句, C#语言提供了一个for语句循环的捷径,而且还促进了集合类的更为一致,就是本文提到的foreach语句,感兴趣的小伙伴们可以参...

    C#教程网11442021-11-03
  • C#C#中math类的全部运算方法(总结)

    C#中math类的全部运算方法(总结)

    下面小编就为大家带来一篇C#中math类的全部运算方法(总结)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    C#教程网10532021-12-07
  • C#C# ping网络IP 实现网络状态检测的方法

    C# ping网络IP 实现网络状态检测的方法

    下面小编就为大家带来一篇C# ping网络IP 实现网络状态检测的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    C#教程网6322021-12-02
  • C#C#通过第三方组件生成二维码(QR Code)和条形码(Bar Code)

    C#通过第三方组件生成二维码(QR Code)和条形码(Bar Code)

    用C#如何生成二维码,我们可以通过现有的第三方dll直接来实现,下面列出几种不同的生成方法...

    C#教程网8222021-12-13