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

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

服务器之家 - 编程语言 - C# - Unity3D实现分页系统

Unity3D实现分页系统

2022-09-01 11:20即步 C#

这篇文章主要为大家详细介绍了Unity3D实现分页系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

在有些情况下,有很多列表不能一次性显示完整,需要对其进行分页处理

博主自己也写了一个分页系统,在这里记录下来,方便以后直接拿来使用

这篇文章Demo也将上传给大家下载参考:点击打开链接

先展示下效果图:

Unity3D实现分页系统

·效果图一

Unity3D实现分页系统

·效果图二

总的来说,要考虑到的逻辑情况还是有点的

工程目录结构如下图:

Unity3D实现分页系统

在每个UIPage下有一个Image框,用来编辑当前是那一页,默认activate=false

整个思路是当点击UIPage获取里面的页数数值,根据这个数值刷新下UIPage的Text值

在确定哪个UIPage下的Image的activate为true

将以下脚本组件挂载到UIPage上

?
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
 
public class UIPage : EventTrigger
{
 public Image image = null;
 public Image GetImage
 {
 get
 {
 if (image = null)
 {
 image = this.transform.GetChild(0).GetComponent<Image>();
 }
 return image;
 }
 set
 {
 image = value;
 }
 }
 
 public Text text = null;
 public Text GetText
 {
 get
 {
 if (text = null)
 {
 text = this.transform.GetChild(1).GetComponent<Text>();
 }
 return text;
 }
 set
 {
 text = value;
 }
 }
 
 
 //点击UI_Page
 public override void OnPointerClick(PointerEventData eventData)
 {
 if (this.transform.GetChild(1).GetComponent<Text>().text == "..." || this.transform.GetChild(1).GetComponent<Text>().text == "")
 {
 return;
 }
 
 PageGrid pg = PageGrid.GetInstance;
 
 //如果点击的是前面几个ui(点击的是1-5)
 if (int.Parse(this.transform.GetChild(1).GetComponent<Text>().text) < PageGrid.GetInstance.uiPageArray.Length)
 {
 string text = this.transform.GetChild(1).GetComponent<Text>().text;
 
 //更新显示
 PageGrid.GetInstance.UpadateUIPageFromHead();
 
 UIPage uiPage = PageGrid.GetInstance.FindUIPageWithText(text);
 if (uiPage)
 {
 PageGrid.GetInstance.ActivatUIPageImage(this.gameObject);
 }
 }
 //点击最后几个(点击的是最后4个)
 else if (int.Parse(this.transform.GetChild(1).GetComponent<Text>().text) >= PageGrid.GetInstance.maxPageIndex - 3)
 {
 string text = this.transform.GetChild(1).GetComponent<Text>().text;
 
 //更新显示
 PageGrid.GetInstance.UpdateUIPageFromEnd();
 
 UIPage uiPage = PageGrid.GetInstance.FindUIPageWithText(text);
 if (uiPage)
 {
 PageGrid.GetInstance.ActivatUIPageImage(uiPage.gameObject);
 }
 }
 else
 {
 string text = this.transform.GetChild(1).GetComponent<Text>().text;
 
 //更新显示
 PageGrid.GetInstance.UpdateUIPageFromMiddle(text);
 
 /*由于数字向后移动,故image显示位置不需要改变*/
 }
 }
}

 

在做完了UIPage的点击事件后,需要实现页面跳转(左右按钮的点击实际也是一个跳转)

?
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 
/// <summary>
/// 管理UIPage
/// </summary>
public class PageGrid : MonoBehaviour
{
 //在初始化时最大的页数
 public int maxPageIndex = 100;
 
 
 [HideInInspector]
 public UIPage[] uiPageArray { get; set; }
 
 private static PageGrid _instance;
 public static PageGrid GetInstance
 {
 get
 {
 if (_instance == null)
 {
 _instance = GameObject.FindGameObjectWithTag("pageGrid").GetComponent<PageGrid>();
 }
 
 return _instance;
 }
 }
 
 private void Start()
 {
 //获取其子节点UIPage组件
 uiPageArray = this.GetComponentsInChildren<UIPage>();
 
 //初始化子节点ui显示
 UpadateUIPageFromHead();
 }
 
 /// <summary>
 /// 在UIPage上更新
 /// </summary>
 public void UpadateUIPageFromHead()
 {
 //从一开始计数
 int headPageIndex = 1;
 
 int n_pageHeadIndex = headPageIndex;
 
 //页数大于UIPage数(大于6)
 if (maxPageIndex > uiPageArray.Length)
 {
 foreach (var item in uiPageArray)
 {
 //倒数第二个
 if (headPageIndex - n_pageHeadIndex == uiPageArray.Length - 2)
 {
  item.transform.GetChild(1).GetComponent<Text>().text = "...";
 }
 //倒数第一个
 else if (headPageIndex - n_pageHeadIndex == uiPageArray.Length - 1)
 {
  item.transform.GetChild(1).GetComponent<Text>().text = maxPageIndex.ToString();
 }
 else
 {
  item.transform.GetChild(1).GetComponent<Text>().text = headPageIndex.ToString();
 }
 
 headPageIndex++;
 }
 }
 //页数等于UIPage数
 else if (maxPageIndex == uiPageArray.Length)
 {
 foreach (var item in uiPageArray)
 {
 item.transform.GetChild(1).GetComponent<Text>().text = headPageIndex.ToString();
 
 headPageIndex++;
 }
 }
 else
 {
 for (int i = 0; i < maxPageIndex; i++)
 {
 uiPageArray[i].transform.GetChild(1).GetComponent<Text>().text = headPageIndex.ToString();
 
 headPageIndex++;
 }
 }
 }
 
 
 /// <summary>
 /// 在UIPage上更新
 /// </summary>
 public void UpdateUIPageFromEnd()
 {
 //页数大于UIPage数(大于6)
 if (maxPageIndex > uiPageArray.Length)
 {
 int count = maxPageIndex;
 for (int i = uiPageArray.Length - 1; i > 0; i--)
 {
 if (i == 0)
 {
  uiPageArray[i].transform.GetChild(1).GetComponent<Text>().text = "1";
 }
 else if (i == 1)
 {
  uiPageArray[i].transform.GetChild(1).GetComponent<Text>().text = "...";
 }
 else
 {
  uiPageArray[i].transform.GetChild(1).GetComponent<Text>().text = count.ToString();
  count--;
 }
 }
 }
 else
 {
 int count = 1;
 for (int i = 0; i < maxPageIndex; i++)
 {
 uiPageArray[i].transform.GetChild(1).GetComponent<Text>().text = count.ToString();
 count++;
 }
 }
 }
 
 /// <summary>
 /// 在UIPage中间更新
 /// </summary>
 public void UpdateUIPageFromMiddle(string number)
 {
 int count = int.Parse(number);
 for (int i = 0; i < uiPageArray.Length; i++)
 {
 if (i == 0)
 {
 uiPageArray[i].transform.GetChild(1).GetComponent<Text>().text = "1";
 }
 else if (i == 1 || i == uiPageArray.Length - 2)
 {
 uiPageArray[i].transform.GetChild(1).GetComponent<Text>().text = "...";
 }
 else if (i == uiPageArray.Length - 1)
 {
 uiPageArray[i].transform.GetChild(1).GetComponent<Text>().text = maxPageIndex.ToString();
 }
 else
 {
 uiPageArray[i].transform.GetChild(1).GetComponent<Text>().text = count.ToString();
 count++;
 }
 }
 }
 
 
 
 //需要和服务器交互TODO
 public void ActivatUIPageImage(GameObject uiPage)
 {
 //将全部uiPage的Image取消激活
 foreach (var item in uiPageArray)
 {
 item.transform.GetChild(0).gameObject.SetActive(false);
 }
 
 uiPage.transform.GetChild(0).gameObject.SetActive(true);
 }
 
 /// <summary>
 /// 按文本内容查询
 /// </summary>
 /// <param name="text"></param>
 public UIPage FindUIPageWithText(string text)
 {
 foreach (var item in uiPageArray)
 {
 if (item.transform.GetChild(1).GetComponent<Text>().text == text)
 {
 return item;
 }
 }
 
 return null;
 }
 
 private UIPage FindUIPageWithImage()
 {
 foreach (var item in uiPageArray)
 {
 if (item.transform.GetChild(0).gameObject.activeInHierarchy)
 {
 return item;
 }
 }
 
 return null;
 }
 
 
 /// <summary>
 /// 页面跳转
 /// </summary>
 public void JumpPage()//这里用于输入框回车事件
 {
 //获取输入信息
 string number = GameObject.FindGameObjectWithTag("PageInputField").GetComponent<InputField>().text;
 if (string.IsNullOrEmpty(number))
 {
 return;
 }
 
 
 //查询前面几个ui(点击的是1-4)
 if (int.Parse(number) < PageGrid.GetInstance.uiPageArray.Length - 1)
 {
 PageGrid.GetInstance.UpadateUIPageFromHead();
 
 UIPage uiPage = PageGrid.GetInstance.FindUIPageWithText(number);
 if (uiPage)
 {
 GameObject obj = uiPage.gameObject;
 
 PageGrid.GetInstance.ActivatUIPageImage(obj);
 }
 }
 //查询最后几个(点击的是最后4个)
 else if (int.Parse(number) >= PageGrid.GetInstance.maxPageIndex - 3)
 {
 PageGrid.GetInstance.UpdateUIPageFromEnd();
 
 UIPage uiPage = PageGrid.GetInstance.FindUIPageWithText(number);
 if (uiPage)
 {
 GameObject obj = uiPage.gameObject;
 
 PageGrid.GetInstance.ActivatUIPageImage(obj);
 }
 }
 else
 {
 UpdateUIPageFromMiddle(number);
 
 UIPage uiPage = PageGrid.GetInstance.FindUIPageWithText(number);
 if (uiPage)
 {
 GameObject obj = uiPage.gameObject;
 
 PageGrid.GetInstance.ActivatUIPageImage(obj);
 }
 }
 }
 
 
 /// <summary>
 /// 跳转
 /// </summary>
 /// <param name="str"></param>
 public void JumpPage(string str)
 {
 //获取输入信息
 string number = str;
 
 
 //查询前面几个ui(点击的是1-4)
 if (int.Parse(number) < PageGrid.GetInstance.uiPageArray.Length - 1)
 {
 PageGrid.GetInstance.UpadateUIPageFromHead();
 
 UIPage uiPage = PageGrid.GetInstance.FindUIPageWithText(number);
 if (uiPage)
 {
 GameObject obj = uiPage.gameObject;
 
 PageGrid.GetInstance.ActivatUIPageImage(obj);
 }
 }
 //查询最后几个(点击的是最后4个)
 else if (int.Parse(number) >= PageGrid.GetInstance.maxPageIndex - 3)
 {
 PageGrid.GetInstance.UpdateUIPageFromEnd();
 
 UIPage uiPage = PageGrid.GetInstance.FindUIPageWithText(number);
 if (uiPage)
 {
 GameObject obj = uiPage.gameObject;
 
 PageGrid.GetInstance.ActivatUIPageImage(obj);
 }
 }
 else
 {
 UpdateUIPageFromMiddle(number);
 
 UIPage uiPage = PageGrid.GetInstance.FindUIPageWithText(number);
 if (uiPage)
 {
 GameObject obj = uiPage.gameObject;
 
 PageGrid.GetInstance.ActivatUIPageImage(obj);
 }
 }
 }
 
 /// <summary>
 /// 页面选择按钮
 /// </summary>
 /// <param name="selection">(向左:-1)( 向右:1)</param>
 public void OnBtnRight(string selection)
 {
 UIPage uiPage = PageGrid.GetInstance.FindUIPageWithImage();
 if (uiPage)
 {
 //当前页面是最后一页或者是第一页
 if (int.Parse(uiPage.transform.GetChild(1).GetComponent<Text>().text) == maxPageIndex && selection == "1" || int.Parse(uiPage.transform.GetChild(1).GetComponent<Text>().text) == 1 && selection == "-1")
 {
 return;
 }
 else
 {
 //跳转页面
 JumpPage((int.Parse(uiPage.transform.GetChild(1).GetComponent<Text>().text) + int.Parse(selection)).ToString());
 }
 }
 }
}

将该脚本挂载到父节点pageGrid上

Unity3D实现分页系统

最后需要将条形框回车事件绑定上去

Unity3D实现分页系统

这样一个完成简单分页系统

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

原文链接:https://blog.csdn.net/qq_33747722/article/details/74924458

延伸 · 阅读

精彩推荐