RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
unity如何实现翻页按钮功能

这篇文章主要为大家展示了unity如何实现翻页按钮功能,内容简而易懂,希望大家可以学习一下,学习完之后肯定会有收获的,下面让小编带大家一起来看看吧。

目前创新互联建站已为近1000家的企业提供了网站建设、域名、虚拟主机、网站托管、服务器托管、企业网站设计、云溪网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

效果图:

unity如何实现翻页按钮功能

UI子父级关系:

unity如何实现翻页按钮功能

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

脚本: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 < 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();
 
  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

以上就是关于unity如何实现翻页按钮功能的内容,如果你们有学习到知识或者技能,可以把它分享出去让更多的人看到。


本文题目:unity如何实现翻页按钮功能
本文链接:http://lswzjz.com/article/jijhhp.html