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

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

服务器之家 - 编程语言 - C# - unity实现翻页按钮功能

unity实现翻页按钮功能

2022-09-02 11:37贪玩的孩纸时代 C#

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

本文实例为大家分享了unity实现翻页按钮功能的具体代码,供大家参考,具体内容如下

效果图:

unity实现翻页按钮功能

UI子父级关系:

unity实现翻页按钮功能

代码中也都有加入注释,有不懂可私信我。脚本中用到了对象池,我没有上传,可根据自己需求做相应变动。

脚本:PageBtnPanelC

?
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
/// <summary>
/// 分页按钮面板控制器
/// </summary>
public class PageBtnPanelC : MonoBehaviour {
 private HorizontalLayoutGroup self_HLG;
 /// <summary>
 /// 上一页按钮
 /// </summary>
 private Button lastPageBtn;
 /// <summary>
 /// 下一页按钮
 /// </summary>
 private Button nextPageBtn;
 /// <summary>
 /// 页数的父物体
 /// </summary>
 private RectTransform pageBtnParent;
 private HorizontalLayoutGroup pageBtnParent_HLG;
 /// <summary>
 /// 上一页按钮点击事件
 /// </summary>
 private UnityAction<int> lastPageBtnEvent;
 /// <summary>
 /// 下一页按钮点击事件
 /// </summary>
 private UnityAction<int> nextPageBtnEvent;
 /// <summary>
 /// 当前显示页面的下标
 /// </summary>
 private int _currentShowPageIndex = 1;
 public int CurrentShowPageIndex {
  get {
   return _currentShowPageIndex;
  }
  set {
   _currentShowPageIndex = value;
  }
 }
 /// <summary>
 /// 总的页面数
 /// </summary>
 private int totalPageNumber = 0;
 /// <summary>
 /// 显示按钮的个数 奇数个
 /// </summary>
 [Header("必须是奇数个,且小于总页数")]
 private int _showBtnCount = 5;
 public int ShowBtnCount {
  get {
   if (_showBtnCount > totalPageNumber) {
    _showBtnCount = totalPageNumber;
   }
 
   return _showBtnCount;
  }
  set {
   if (value % 2 == 0)
   {
    _showBtnCount = value - 1;
   }
   else {
    _showBtnCount = value;
   }
  }
 }
 /// <summary>
 /// 页数按钮 预设体
 /// </summary>
 public GameObject btnPrefabs;
 /// <summary>
 /// 页数按钮 存放list
 /// </summary>
 public List<PageBtnC> pbcList;
 
 private void Start()
 {
  Init();
  Set(14, 9, (index1) =>
  {
   Debug.Log("当前显示第:" + CurrentShowPageIndex + "页");
  }, (index2) =>
  {
   Debug.Log("当前显示第:" + CurrentShowPageIndex + "页");
  }, (_pageIndex, _pbc) =>
  {
   Debug.Log("当前显示第:" + CurrentShowPageIndex + "页");
  });
 }
 /// <summary>
 /// 改变显示的状态
 /// </summary>
 void ChangeShowState() {
  int _showBtnCount = ShowBtnCount;
  /*
   判断是否在可更新范围内,如果在更新范围内,则将CurrentShowPageIndex设置为中心位置的按钮
   eg:假设总共有10页(totalPageNumber = 10),显示按钮的个数为7(ShowBtnCount = 7)
   则应该在 (ShowBtnCount / 2 + 1) = 4 到 totalPageNumber - ShowBtnCount / 2 = 7 之间设置
   如果CurrentShowPageIndex = 5或6
   则应该这样显示  1.. 3 4 5 6 7 ..10
   如果不在更新范围内,
   如果CurrentShowPageIndex = 1或2或3或4  则应该这样显示: 1 2 3 4 5 6 ..10
   如果如果CurrentShowPageIndex = 7或8或9或10 则应该这样显示 1.. 5 6 7 8 9 10
   */
  if (CurrentShowPageIndex >= (ShowBtnCount / 2 + 1) && CurrentShowPageIndex <= (totalPageNumber - ShowBtnCount / 2))
  {
   int _showBtnCount2 = _showBtnCount - 2;
   _showBtnCount2 /= 2;
 
   //判断起始下标
   int startIndex = CurrentShowPageIndex - _showBtnCount2;
   int endIndex = CurrentShowPageIndex + _showBtnCount2;
 
   //防止超出
   if (startIndex <= 1)
   {
    startIndex = 2;
   }
   //防止超出
   if (endIndex >= totalPageNumber)
   {
    endIndex = totalPageNumber - 1;
   }
 
   //计算中心位置按钮的下标 因为showBtnCount不定
   int centerIndex = ShowBtnCount / 2;
 
   pbcList[centerIndex].Set(CurrentShowPageIndex);
 
   //循环设置前面一部分按钮信息
   for (int i = 1; i < centerIndex; i++)
   {
    pbcList[i].Set(startIndex++);
   }
 
   //循环设置后面一部分按钮信息
   for (int i = centerIndex + 1; i < ShowBtnCount - 1; i++)
   {
    startIndex++;
    pbcList[i].Set(startIndex);
   }
  }
  else {
   //如果点击的是小于等于4的按钮下标
   if (CurrentShowPageIndex < (ShowBtnCount / 2 + 1))
   {
    for (int i = 0; i < ShowBtnCount - 1; i++) {
     pbcList[i].Set(i+1);
    }
   }//如果点击的事大于等于7的按钮下标
   else if (CurrentShowPageIndex > (totalPageNumber - ShowBtnCount / 2)) {
 
    int startNumber = totalPageNumber - ShowBtnCount + 2;
 
    for (int i = 1; i < ShowBtnCount; i++) {
     pbcList[i].Set(startNumber++);
    }
   }
  }
 
  /*
   判断总显示页数是否大于显示页数
   以防止出现这种效果:
   例如:totalPageNumber = 7,ShowBtnCount =7
   防止出现的效果:1 2 3 4 5 6 ..7 和 1.. 2 3 4 5 6 7
   应该出现的效果:1 2 3 4 5 6 7
   */
  if (totalPageNumber > ShowBtnCount){
   _showBtnCount -= 2;
   _showBtnCount /= 2;
   if (CurrentShowPageIndex - _showBtnCount - 1 > 1)
   {
    pbcList[0].Set(1, "1..");
   }
   else
   {
    pbcList[0].Set(1);
   }
   if (CurrentShowPageIndex + _showBtnCount + 1 < totalPageNumber)
   {
    pbcList[ShowBtnCount - 1].Set(totalPageNumber, ".." + totalPageNumber);
   }
   else
   {
    pbcList[ShowBtnCount - 1].Set(totalPageNumber);
   }
  }
 }
 
 private bool isInit = false;
 public void Init() {
  if (isInit)
   return;
  isInit = true;
 
  pbcList = new List<PageBtnC>();
 
  self_HLG = transform.GetComponent<HorizontalLayoutGroup>();
 
  pageBtnParent = transform.Find("PageIndexParent") as RectTransform;
  pageBtnParent_HLG = pageBtnParent.GetComponent<HorizontalLayoutGroup>();
 
  lastPageBtn = transform.Find("LastPageBtn").GetComponent<Button>();
  lastPageBtn.onClick.AddListener(()=> {
   if (totalPageNumber <= 0)
    return;
 
   if (CurrentShowPageIndex > 1) {
    CurrentShowPageIndex--;
   }
 
   ResetPageBtnState();
 
   ChangeShowState();
 
   PageBtnC pbc = GetPBCFromIndex(CurrentShowPageIndex);
   if (pbc != null) {
    pbc.SetHighlightColor();
   }
 
   if (lastPageBtnEvent != null)
   {
    lastPageBtnEvent(CurrentShowPageIndex);
   }
  });
 
  nextPageBtn = transform.Find("NextPageBtn").GetComponent<Button>();
  nextPageBtn.onClick.AddListener(()=> {
   if (totalPageNumber <= 0)
    return;
 
   if (CurrentShowPageIndex < totalPageNumber) {
    CurrentShowPageIndex++;
   }
 
   ResetPageBtnState();
 
   ChangeShowState();
   
   PageBtnC pbc = GetPBCFromIndex(CurrentShowPageIndex);
   if (pbc != null)
   {
    pbc.SetHighlightColor();
   }
 
   if (nextPageBtnEvent != null)
   {
    nextPageBtnEvent(CurrentShowPageIndex);
   }
 
  });
 }
 /// <summary>
 /// 设置信息
 /// </summary>
 /// <param name="totalPageNumber">总页数</param>
 /// <param name="showBtnCount">显示多少个按钮,填奇数,如果填偶数,则会强制减1,如:填6,则实际为5</param>
 /// <param name="lastBtnEvent">上一页按钮事件</param>
 /// <param name="nextBtnEvent">下一页按钮事件</param>
 /// <param name="pageBtnClickEvent">单独点击页面按钮事件</param>
 public void Set(int totalPageNumber,int showBtnCount,UnityAction<int> lastBtnEvent,UnityAction<int> nextBtnEvent,UnityAction<int,PageBtnC> pageBtnClickEvent) {
  if (totalPageNumber <= 0)
  {
   this.totalPageNumber = 1;
  }
  else {
   this.totalPageNumber = totalPageNumber;
  }
  
  this.ShowBtnCount = showBtnCount;
 
  this.lastPageBtnEvent = lastBtnEvent;
  this.nextPageBtnEvent = nextBtnEvent;
 
  CurrentShowPageIndex = 1;
 
  pbcList.Clear();
 
  for (int i = 1; i <= ShowBtnCount; i++) {
   int index = i;
   PageBtnC pbc = PoolManager.Instance.Spawn(btnPrefabs, pageBtnParent).GetComponent<PageBtnC>();
   if (pbc) {
    pbc.Set(index,null, (pageIndex,pbc111) =>
    {
     CurrentShowPageIndex = pageIndex;
 
     ResetPageBtnState();
 
     ChangeShowState();
 
     PageBtnC pbc1 = GetPBCFromIndex(CurrentShowPageIndex);
     if (pbc1 != null)
     {
      pbc1.SetHighlightColor();
     }
     if (pageBtnClickEvent != null) {
      pageBtnClickEvent(pageIndex, pbc111);
     }
    });
   }
   pbcList.Add(pbc);
  }
 
  pbcList[0].SetHighlightColor();
 
  ChangeShowState();
 
  Util.SetWidth_ChildWidthSame(pageBtnParent_HLG, pageBtnParent);
 
  Util.SetWidth_ChildWidthNotSame(self_HLG, transform as RectTransform);
 }
 /// <summary>
 /// 重置所有按钮的状态
 /// </summary>
 void ResetPageBtnState() {
  for (int i = 0; i < pbcList.Count; i++) {
   pbcList[i].SetNormalColor();
  }
 }
 /// <summary>
 /// 回收所有页码
 /// </summary>
 public void Unspawn() {
  for (int i = pageBtnParent.childCount - 1; i >= 0; i--) {
   
   PoolManager.Instance.UnSpawn(pageBtnParent.GetChild(i).gameObject);
  }
 }
 
 public void Clear() {
  lastPageBtnEvent = null;
  nextPageBtnEvent = null;
 }
 /// <summary>
 /// 根据index得到pagebtnc物体
 /// </summary>
 /// <param name="index"></param>
 /// <returns></returns>
 PageBtnC GetPBCFromIndex(int index) {
  for (int i = 0; i < pbcList.Count; i++) {
   if (pbcList[i].CurrentPageIndex.Equals(index)) {
    return pbcList[i];
   }
  }
 
  return null;
 }
}
 
public class Util
{
 /// <summary>
 /// 设置物体宽度 子物体宽度相同
 /// </summary>
 /// <param name="parentHLG"></param>
 /// <param name="parent"></param>
 /// <param name="callback"></param>
 public static void SetWidth_ChildWidthSame(HorizontalLayoutGroup parentHLG, RectTransform parent, UnityAction<float> endCallBack = null)
 {
  float width = 0;
 
  float leftPadding = parentHLG.padding.left;
  float spacing = parentHLG.spacing;
 
  int childCount = parent.childCount;
 
  if (childCount > 0)
  {
   RectTransform singleChildRT = parent.GetChild(0) as RectTransform;
 
   width = childCount * singleChildRT.rect.width + (childCount - 1) * spacing + leftPadding;
  }
 
  parent.sizeDelta = new Vector2(width, parent.sizeDelta.y);
 
  if (endCallBack != null)
  {
   endCallBack(width);
  }
 }
 
 /// <summary>
 /// 设置物体宽度 子物体宽度不同
 /// </summary>
 /// <param name="parentHLG"></param>
 /// <param name="parent"></param>
 /// <param name="callback"></param>
 public static void SetWidth_ChildWidthNotSame(HorizontalLayoutGroup parentHLG, RectTransform parent, UnityAction<float> endCallBack = null)
 {
  float width = 0;
 
  RectOffset Padding = parentHLG.padding;
  float spacing = parentHLG.spacing;
 
  int childCount = parent.childCount;
 
  if (childCount > 0)
  {
   for (int i = 0; i < childCount; i++)
   {
    RectTransform childRT = parent.GetChild(i) as RectTransform;
    width += childRT.rect.width;
   }
 
   width += (childCount - 1) * spacing + Padding.left + Padding.right;
  }
 
  parent.sizeDelta = new Vector2(width, parent.sizeDelta.y);
 
  if (endCallBack != null)
  {
   endCallBack(width);
  }
 }
}

脚本:PageBtnC

?
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
/// <summary>
/// 页码按钮控制器
/// </summary>
public class PageBtnC : MonoBehaviour,IPool {
 /// <summary>
 /// 自己显示的页码
 /// </summary>
 private int currentPageIndex = 0;
 public int CurrentPageIndex {
  get {
   return currentPageIndex;
  }
 }
 
 private Button SelfBtn;
 private Image img;
 private Text selfText;
 
 /// <summary>
 /// 按钮事件
 /// </summary>
 public UnityAction<int,PageBtnC> btnClickEvent;
 
 /// <summary>
 /// 按钮正常和高亮颜色
 /// </summary>
 public Color normalColor;
 public Color highlightColor;
 
 /// <summary>
 /// 文本正常和高亮颜色
 /// </summary>
 public Color normalTextColor;
 public Color highlightTextColor;
 
 private bool isInit = false;
 void Init() {
  if (isInit)
   return;
  isInit = true;
 
  img = transform.GetComponent<Image>();
 
  selfText = transform.Find("Text").GetComponent<Text>();
 
  SelfBtn = transform.GetComponent<Button>();
  SelfBtn.onClick.AddListener(()=> {
   
   if (btnClickEvent != null) {
    btnClickEvent(currentPageIndex,this);
   }
  });
 }
 /// <summary>
 /// 设置正常颜色
 /// </summary>
 public void SetNormalColor() {
  img.color = normalColor;
  selfText.color = normalTextColor;
 }
 /// <summary>
 /// 设置高亮颜色
 /// </summary>
 public void SetHighlightColor() {
  img.color = highlightColor;
  selfText.color = highlightTextColor;
 }
 /// <summary>
 /// 设置事件 文本内容等
 /// </summary>
 /// <param name="currentPageIndex"></param>
 /// <param name="selfTextStr"></param>
 /// <param name="btnClickEvent"></param>
 public void Set(int currentPageIndex,string selfTextStr = null,UnityAction<int,PageBtnC> btnClickEvent = null) {
  this.currentPageIndex = currentPageIndex;
 
  if (btnClickEvent != null)
  {
   this.btnClickEvent = btnClickEvent;
  }
 
  if (string.IsNullOrEmpty(selfTextStr))
  {
   selfText.text = currentPageIndex.ToString();
  }
  else {
   selfText.text = selfTextStr;
  }
 }
 
 #region 对象池接口方法
 public int GetMaxCount()
 {
  return 10;
 }
 
 public string SingletonName()
 {
  return this.GetType().Name;
 }
 
 public void Spawn()
 {
  Init();
 }
 
 public void UnSpawn()
 {
  SetNormalColor();
 }
 #endregion
}

github地址

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

原文链接:https://blog.csdn.net/yiwei151/article/details/88406539

延伸 · 阅读

精彩推荐
  • C#C#实现分页组件的方法

    C#实现分页组件的方法

    这篇文章主要为大家详细介绍了C#实现分页组件的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    风雪幻林6442022-01-20
  • C#C#编程实现对象与JSON串互相转换实例分析

    C#编程实现对象与JSON串互相转换实例分析

    这篇文章主要介绍了C#编程实现对象与JSON串互相转换的方法,结合实例分析了在DoNet2.0与Donet3.5环境下实现对象与JSON转换的相关技巧,需要的朋友可以参考下...

    Jan.David11692021-11-02
  • C#C#实现char字符数组与字符串相互转换的方法

    C#实现char字符数组与字符串相互转换的方法

    这篇文章主要介绍了C#实现char字符数组与字符串相互转换的方法,结合实例形式简单分析了C#字符数组转字符串及字符串转字符数组的具体实现技巧,需要的朋...

    Mr-Robot11442021-12-23
  • C#C#基于COM方式读取Excel表格的方法

    C#基于COM方式读取Excel表格的方法

    这篇文章主要介绍了C#基于COM方式读取Excel表格的方法,涉及C# COM组件的调用与Excel表格的使用技巧,需要的朋友可以参考下...

    kagula8562021-11-30
  • C#C# 创建EXCEL图表并保存为图片的实例

    C# 创建EXCEL图表并保存为图片的实例

    下面小编就为大家分享一篇C# 创建EXCEL图表并保存为图片的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    E-iceblue3982022-02-15
  • C#c#在WebAPI使用Session的方法

    c#在WebAPI使用Session的方法

    这篇文章主要介绍了c#在WebAPI使用Session的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    天涯过者10912022-02-25
  • C#C#客户端程序调用外部程序的3种实现方法

    C#客户端程序调用外部程序的3种实现方法

    这篇文章主要给大家介绍了关于C#客户端程序调用外部程序的3种实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习...

    _AlexYIN4372022-02-22
  • C#C#中split用法实例总结

    C#中split用法实例总结

    这篇文章主要介绍了C#中split用法,结合实例形式总结分析了C#常见的split操作字符串相关技巧,需要的朋友可以参考下...

    雨竹9572021-11-26