本文实例为大家分享了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 UnityActionlastPageBtnEvent; /// /// 下一页按钮点击事件 /// private UnityActionnextPageBtnEvent; /// /// 当前显示页面的下标 /// 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 ListpbcList; 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; 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++); } } } 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(); 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 UnityActionbtnClickEvent; /// /// 按钮正常和高亮颜色 /// 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 (); SelfBtn.onClick.AddListener(()=> { if (btnClickEvent != null) { btnClickEvent(currentPageIndex,this); } }); } /// /// 设置正常颜色 /// public void SetNormalColor() { img.color = normalColor; selfText.color = normalTextColor; } ////// 设置高亮颜色 /// public void SetHighlightColor() { img.color = highlightColor; selfText.color = highlightTextColor; } ////// 设置事件 文本内容等 /// /// /// /// public void Set(int currentPageIndex,string selfTextStr = null,UnityActionbtnClickEvent = 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地址
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



