热门标签 | HotTags
当前位置:  开发笔记 > 运维 > 正文

Unity3D实现分页系统

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

在有些情况下,有很多列表不能一次性显示完整,需要对其进行分页处理

博主自己也写了一个分页系统,在这里记录下来,方便以后直接拿来使用

这篇文章Demo也将上传给大家下载参考:点击打开链接

先展示下效果图:

·效果图一

·效果图二

总的来说,要考虑到的逻辑情况还是有点的

工程目录结构如下图:

在每个UIPage下有一个Image框,用来编辑当前是那一页,默认activate=false

整个思路是当点击UIPage获取里面的页数数值,根据这个数值刷新下UIPage的Text值

在确定哪个UIPage下的Image的activate为true

将以下脚本组件挂载到UIPage上

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
 
public class UIPage : EventTrigger
{
 public Image image = null;
 public Image GetImage
 {
 get
 {
 if (image = null)
 {
 image = this.transform.GetChild(0).GetComponent();
 }
 return image;
 }
 set
 {
 image = value;
 }
 }
 
 public Text text = null;
 public Text GetText
 {
 get
 {
 if (text = null)
 {
 text = this.transform.GetChild(1).GetComponent();
 }
 return text;
 }
 set
 {
 text = value;
 }
 }
 
 
 //点击UI_Page
 public override void OnPointerClick(PointerEventData eventData)
 {
 if (this.transform.GetChild(1).GetComponent().text == "..." || this.transform.GetChild(1).GetComponent().text == "")
 {
 return;
 }
 
 PageGrid pg = PageGrid.GetInstance;
 
 //如果点击的是前面几个ui(点击的是1-5)
 if (int.Parse(this.transform.GetChild(1).GetComponent().text) ().text;
 
 //更新显示
 PageGrid.GetInstance.UpadateUIPageFromHead();
 
 UIPage uiPage = PageGrid.GetInstance.FindUIPageWithText(text);
 if (uiPage)
 {
 PageGrid.GetInstance.ActivatUIPageImage(this.gameObject);
 }
 }
 //点击最后几个(点击的是最后4个)
 else if (int.Parse(this.transform.GetChild(1).GetComponent().text) >= PageGrid.GetInstance.maxPageIndex - 3)
 {
 string text = this.transform.GetChild(1).GetComponent().text;
 
 //更新显示
 PageGrid.GetInstance.UpdateUIPageFromEnd();
 
 UIPage uiPage = PageGrid.GetInstance.FindUIPageWithText(text);
 if (uiPage)
 {
 PageGrid.GetInstance.ActivatUIPageImage(uiPage.gameObject);
 }
 }
 else
 {
 string text = this.transform.GetChild(1).GetComponent().text;
 
 //更新显示
 PageGrid.GetInstance.UpdateUIPageFromMiddle(text);
 
 /*由于数字向后移动,故image显示位置不需要改变*/
 }
 }
}

在做完了UIPage的点击事件后,需要实现页面跳转(左右按钮的点击实际也是一个跳转)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 
/// 
/// 管理UIPage
/// 
public class PageGrid : MonoBehaviour
{
 //在初始化时最大的页数
 public int maxPageIndex = 100;
 
 
 [HideInInspector]
 public UIPage[] uiPageArray { get; set; }
 
 private static PageGrid _instance;
 public static PageGrid GetInstance
 {
 get
 {
 if (_instance == null)
 {
 _instance = GameObject.FindGameObjectWithTag("pageGrid").GetComponent();
 }
 
 return _instance;
 }
 }
 
 private void Start()
 {
 //获取其子节点UIPage组件
 uiPageArray = this.GetComponentsInChildren();
 
 //初始化子节点ui显示
 UpadateUIPageFromHead();
 }
 
 /// 
 /// 在UIPage上更新
 /// 
 public void UpadateUIPageFromHead()
 {
 //从一开始计数
 int headPageIndex = 1;
 
 int n_pageHeadIndex = headPageIndex;
 
 //页数大于UIPage数(大于6)
 if (maxPageIndex > uiPageArray.Length)
 {
 foreach (var item in uiPageArray)
 {
 //倒数第二个
 if (headPageIndex - n_pageHeadIndex == uiPageArray.Length - 2)
 {
  item.transform.GetChild(1).GetComponent().text = "...";
 }
 //倒数第一个
 else if (headPageIndex - n_pageHeadIndex == uiPageArray.Length - 1)
 {
  item.transform.GetChild(1).GetComponent().text = maxPageIndex.ToString();
 }
 else
 {
  item.transform.GetChild(1).GetComponent().text = headPageIndex.ToString();
 }
 
 headPageIndex++; 
 }
 }
 //页数等于UIPage数
 else if (maxPageIndex == uiPageArray.Length)
 {
 foreach (var item in uiPageArray)
 {
 item.transform.GetChild(1).GetComponent().text = headPageIndex.ToString();
 
 headPageIndex++;
 }
 }
 else
 {
 for (int i = 0; i ().text = headPageIndex.ToString();
 
 headPageIndex++;
 }
 }
 }
 
 
 /// 
 /// 在UIPage上更新
 /// 
 public void UpdateUIPageFromEnd()
 {
 //页数大于UIPage数(大于6)
 if (maxPageIndex > uiPageArray.Length)
 {
 int count = maxPageIndex;
 for (int i = uiPageArray.Length - 1; i > 0; i--)
 {
 if (i == 0)
 {
  uiPageArray[i].transform.GetChild(1).GetComponent().text = "1";
 }
 else if (i == 1)
 {
  uiPageArray[i].transform.GetChild(1).GetComponent().text = "...";
 }
 else
 {
  uiPageArray[i].transform.GetChild(1).GetComponent().text = count.ToString();
  count--;
 }
 }
 }
 else
 {
 int count = 1;
 for (int i = 0; i ().text = count.ToString();
 count++;
 }
 }
 }
 
 /// 
 /// 在UIPage中间更新
 /// 
 public void UpdateUIPageFromMiddle(string number)
 {
 int count = int.Parse(number);
 for (int i = 0; i ().text = "1";
 }
 else if (i == 1 || i == uiPageArray.Length - 2)
 {
 uiPageArray[i].transform.GetChild(1).GetComponent().text = "...";
 }
 else if (i == uiPageArray.Length - 1)
 {
 uiPageArray[i].transform.GetChild(1).GetComponent().text = maxPageIndex.ToString();
 }
 else
 {
 uiPageArray[i].transform.GetChild(1).GetComponent().text = count.ToString();
 count++;
 }
 }
 }
 
 
 
 //需要和服务器交互TODO
 public void ActivatUIPageImage(GameObject uiPage)
 {
 //将全部uiPage的Image取消激活
 foreach (var item in uiPageArray)
 {
 item.transform.GetChild(0).gameObject.SetActive(false);
 }
 
 uiPage.transform.GetChild(0).gameObject.SetActive(true);
 }
 
 /// 
 /// 按文本内容查询
 /// 
 /// 
 public UIPage FindUIPageWithText(string text)
 {
 foreach (var item in uiPageArray)
 {
 if (item.transform.GetChild(1).GetComponent().text == text)
 {
 return item;
 }
 }
 
 return null;
 }
 
 private UIPage FindUIPageWithImage()
 {
 foreach (var item in uiPageArray)
 {
 if (item.transform.GetChild(0).gameObject.activeInHierarchy)
 {
 return item;
 }
 }
 
 return null;
 }
 
 
 /// 
 /// 页面跳转
 /// 
 public void JumpPage()//这里用于输入框回车事件
 {
 //获取输入信息
 string number = GameObject.FindGameObjectWithTag("PageInputField").GetComponent().text;
 if (string.IsNullOrEmpty(number))
 {
 return;
 }
 
 
 //查询前面几个ui(点击的是1-4)
 if (int.Parse(number) = PageGrid.GetInstance.maxPageIndex - 3)
 {
 PageGrid.GetInstance.UpdateUIPageFromEnd();
 
 UIPage uiPage = PageGrid.GetInstance.FindUIPageWithText(number);
 if (uiPage)
 {
 GameObject obj = uiPage.gameObject;
 
 PageGrid.GetInstance.ActivatUIPageImage(obj);
 }
 }
 else
 {
 UpdateUIPageFromMiddle(number);
 
 UIPage uiPage = PageGrid.GetInstance.FindUIPageWithText(number);
 if (uiPage)
 {
 GameObject obj = uiPage.gameObject;
 
 PageGrid.GetInstance.ActivatUIPageImage(obj);
 }
 }
 }
 
 
 /// 
 /// 跳转
 /// 
 /// 
 public void JumpPage(string str)
 {
 //获取输入信息
 string number = str;
 
 
 //查询前面几个ui(点击的是1-4)
 if (int.Parse(number) = PageGrid.GetInstance.maxPageIndex - 3)
 {
 PageGrid.GetInstance.UpdateUIPageFromEnd();
 
 UIPage uiPage = PageGrid.GetInstance.FindUIPageWithText(number);
 if (uiPage)
 {
 GameObject obj = uiPage.gameObject;
 
 PageGrid.GetInstance.ActivatUIPageImage(obj);
 }
 }
 else
 {
 UpdateUIPageFromMiddle(number);
 
 UIPage uiPage = PageGrid.GetInstance.FindUIPageWithText(number);
 if (uiPage)
 {
 GameObject obj = uiPage.gameObject;
 
 PageGrid.GetInstance.ActivatUIPageImage(obj);
 }
 }
 }
 
 /// 
 /// 页面选择按钮
 /// 
 /// (向左:-1)( 向右:1)
 public void OnBtnRight(string selection)
 {
 UIPage uiPage = PageGrid.GetInstance.FindUIPageWithImage();
 if (uiPage)
 {
 //当前页面是最后一页或者是第一页
 if (int.Parse(uiPage.transform.GetChild(1).GetComponent().text) == maxPageIndex && selection == "1" || int.Parse(uiPage.transform.GetChild(1).GetComponent().text) == 1 && selection == "-1")
 {
 return;
 }
 else
 {
 //跳转页面
 JumpPage((int.Parse(uiPage.transform.GetChild(1).GetComponent().text) + int.Parse(selection)).ToString());
 }
 }
 }
}

将该脚本挂载到父节点pageGrid上

最后需要将条形框回车事件绑定上去

这样一个完成简单分页系统

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


推荐阅读
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 本文介绍了在rhel5.5操作系统下搭建网关+LAMP+postfix+dhcp的步骤和配置方法。通过配置dhcp自动分配ip、实现外网访问公司网站、内网收发邮件、内网上网以及SNAT转换等功能。详细介绍了安装dhcp和配置相关文件的步骤,并提供了相关的命令和配置示例。 ... [详细]
  • 如何使用Java获取服务器硬件信息和磁盘负载率
    本文介绍了使用Java编程语言获取服务器硬件信息和磁盘负载率的方法。首先在远程服务器上搭建一个支持服务端语言的HTTP服务,并获取服务器的磁盘信息,并将结果输出。然后在本地使用JS编写一个AJAX脚本,远程请求服务端的程序,得到结果并展示给用户。其中还介绍了如何提取硬盘序列号的方法。 ... [详细]
  • 本文介绍了如何找到并终止在8080端口上运行的进程的方法,通过使用终端命令lsof -i :8080可以获取在该端口上运行的所有进程的输出,并使用kill命令终止指定进程的运行。 ... [详细]
  • 禁止程序接收鼠标事件的工具_VNC Viewer for Mac(远程桌面工具)免费版
    VNCViewerforMac是一款运行在Mac平台上的远程桌面工具,vncviewermac版可以帮助您使用Mac的键盘和鼠标来控制远程计算机,操作简 ... [详细]
  • 本文详细介绍了云服务器API接口的概念和作用,以及如何使用API接口管理云上资源和开发应用程序。通过创建实例API、调整实例配置API、关闭实例API和退还实例API等功能,可以实现云服务器的创建、配置修改和销毁等操作。对于想要学习云服务器API接口的人来说,本文提供了详细的入门指南和使用方法。如果想进一步了解相关知识或阅读更多相关文章,请关注编程笔记行业资讯频道。 ... [详细]
  • 阿,里,云,物,联网,net,core,客户端,czgl,aliiotclient, ... [详细]
  • [译]技术公司十年经验的职场生涯回顾
    本文是一位在技术公司工作十年的职场人士对自己职业生涯的总结回顾。她的职业规划与众不同,令人深思又有趣。其中涉及到的内容有机器学习、创新创业以及引用了女性主义者在TED演讲中的部分讲义。文章表达了对职业生涯的愿望和希望,认为人类有能力不断改善自己。 ... [详细]
  • 如何基于ggplot2构建相关系数矩阵热图以及一个友情故事
    本文介绍了如何在rstudio中安装ggplot2,并使用ggplot2构建相关系数矩阵热图。同时,通过一个友情故事,讲述了真爱难觅的故事背后的数据量化和皮尔逊相关系数的概念。故事中的小伙伴们在本科时参加各种考试,其中有些沉迷网络游戏,有些热爱体育,通过他们的故事,展示了不同兴趣和特长对学习和成绩的影响。 ... [详细]
  • 如何用UE4制作2D游戏文档——计算篇
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了如何用UE4制作2D游戏文档——计算篇相关的知识,希望对你有一定的参考价值。 ... [详细]
  • 本文介绍了使用PHP实现断点续传乱序合并文件的方法和源码。由于网络原因,文件需要分割成多个部分发送,因此无法按顺序接收。文章中提供了merge2.php的源码,通过使用shuffle函数打乱文件读取顺序,实现了乱序合并文件的功能。同时,还介绍了filesize、glob、unlink、fopen等相关函数的使用。阅读本文可以了解如何使用PHP实现断点续传乱序合并文件的具体步骤。 ... [详细]
  • 本文介绍了Hyperledger Fabric外部链码构建与运行的相关知识,包括在Hyperledger Fabric 2.0版本之前链码构建和运行的困难性,外部构建模式的实现原理以及外部构建和运行API的使用方法。通过本文的介绍,读者可以了解到如何利用外部构建和运行的方式来实现链码的构建和运行,并且不再受限于特定的语言和部署环境。 ... [详细]
  • 关于我们EMQ是一家全球领先的开源物联网基础设施软件供应商,服务新产业周期的IoT&5G、边缘计算与云计算市场,交付全球领先的开源物联网消息服务器和流处理数据 ... [详细]
  • 图解redis的持久化存储机制RDB和AOF的原理和优缺点
    本文通过图解的方式介绍了redis的持久化存储机制RDB和AOF的原理和优缺点。RDB是将redis内存中的数据保存为快照文件,恢复速度较快但不支持拉链式快照。AOF是将操作日志保存到磁盘,实时存储数据但恢复速度较慢。文章详细分析了两种机制的优缺点,帮助读者更好地理解redis的持久化存储策略。 ... [详细]
  • http:my.oschina.netleejun2005blog136820刚看到群里又有同学在说HTTP协议下的Get请求参数长度是有大小限制的,最大不能超过XX ... [详细]
author-avatar
小-捌_350
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有