unity实现翻页按钮功能

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

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

效果图:

UI子父级关系:

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

脚本:PageBtnPanelC

 using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; using UnityEngine.UI; ///  /// 分页按钮面板控制器 ///  public class PageBtnPanelC : MonoBehaviour { private HorizontalLayoutGroup self_HLG; ///  /// 上一页按钮 ///  private Button lastPageBtn; ///  /// 下一页按钮 ///  private Button nextPageBtn; ///  /// 页数的父物体 ///  private RectTransform pageBtnParent; private HorizontalLayoutGroup pageBtnParent_HLG; ///  /// 上一页按钮点击事件 ///  private UnityAction lastPageBtnEvent; ///  /// 下一页按钮点击事件 ///  private UnityAction nextPageBtnEvent; ///  /// 当前显示页面的下标 ///  private int _currentShowPageIndex = 1; public int CurrentShowPageIndex { get { return _currentShowPageIndex; } set { _currentShowPageIndex = value; } } ///  /// 总的页面数 ///  private int totalPageNumber = 0; ///  /// 显示按钮的个数 奇数个 ///  [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; } } } ///  /// 页数按钮 预设体 ///  public GameObject btnPrefabs; ///  /// 页数按钮 存放list ///  public List pbcList; private void Start() { Init(); Set(14, 9, (index1) => { Debug.Log("当前显示第:" + CurrentShowPageIndex + "页"); }, (index2) => { Debug.Log("当前显示第:" + CurrentShowPageIndex + "页"); }, (_pageIndex, _pbc) => { Debug.Log("当前显示第:" + CurrentShowPageIndex + "页"); }); } ///  /// 改变显示的状态 ///  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  (totalPageNumber - ShowBtnCount / 2)) { int startNumber = totalPageNumber - ShowBtnCount + 2; for (int i = 1; i  ShowBtnCount){ _showBtnCount -= 2; _showBtnCount /= 2; if (CurrentShowPageIndex - _showBtnCount - 1 > 1) { pbcList[0].Set(1, "1.."); } else { pbcList[0].Set(1); } if (CurrentShowPageIndex + _showBtnCount + 1 (); self_HLG = transform.GetComponent(); pageBtnParent = transform.Find("PageIndexParent") as RectTransform; pageBtnParent_HLG = pageBtnParent.GetComponent(); lastPageBtn = transform.Find("LastPageBtn").GetComponent

脚本:PageBtnC

 using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; using UnityEngine.UI; ///  /// 页码按钮控制器 ///  public class PageBtnC : MonoBehaviour,IPool { ///  /// 自己显示的页码 ///  private int currentPageIndex = 0; public int CurrentPageIndex { get { return currentPageIndex; } } private Button SelfBtn; private Image img; private Text selfText; ///  /// 按钮事件 ///  public UnityAction btnClickEvent; ///  /// 按钮正常和高亮颜色 ///  public Color normalColor; public Color highlightColor; ///  /// 文本正常和高亮颜色 ///  public Color normalTextColor; public Color highlightTextColor; private bool isInit = false; void Init() { if (isInit) return; isInit = true; img = transform.GetComponent(); selfText = transform.Find("Text").GetComponent(); SelfBtn = transform.GetComponent

github地址

以上就是unity实现翻页按钮功能的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 其他教程