栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

unity实现翻页按钮功能

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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;
  
  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 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地址

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/122171.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号