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<Image>();
 }
 return image;
 }
 set
 {
 image = value;
 }
 }

 public Text text = null;
 public Text GetText
 {
 get
 {
 if (text = null)
 {
 text = this.transform.GetChild(1).GetComponent<Text>();
 }
 return text;
 }
 set
 {
 text = value;
 }
 }

 //点击UI_Page
 public override void OnPointerClick(PointerEventData eventData)
 {
 if (this.transform.GetChild(1).GetComponent<Text>().text == "..." || this.transform.GetChild(1).GetComponent<Text>().text == "")
 {
 return;
 }

 PageGrid pg = PageGrid.GetInstance;

 //如果点击的是前面几个ui(点击的是1-5)
 if (int.Parse(this.transform.GetChild(1).GetComponent<Text>().text) < PageGrid.GetInstance.uiPageArray.Length)
 {
 string text = 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>().text) >= PageGrid.GetInstance.maxPageIndex - 3)
 {
 string text = this.transform.GetChild(1).GetComponent<Text>().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>().text;

 //更新显示
 PageGrid.GetInstance.UpdateUIPageFromMiddle(text);

 /*由于数字向后移动,故image显示位置不需要改变*/
 }
 }
}

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

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// 管理UIPage
/// </summary>
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<PageGrid>();
 }

 return _instance;
 }
 }

 private void Start()
 {
 //获取其子节点UIPage组件
 uiPageArray = this.GetComponentsInChildren<UIPage>();

 //初始化子节点ui显示
 UpadateUIPageFromHead();
 }

 /// <summary>
 /// 在UIPage上更新
 /// </summary>
 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>().text = "...";
 }
 //倒数第一个
 else if (headPageIndex - n_pageHeadIndex == uiPageArray.Length - 1)
 {
  item.transform.GetChild(1).GetComponent<Text>().text = maxPageIndex.ToString();
 }
 else
 {
  item.transform.GetChild(1).GetComponent<Text>().text = headPageIndex.ToString();
 }

 headPageIndex++;
 }
 }
 //页数等于UIPage数
 else if (maxPageIndex == uiPageArray.Length)
 {
 foreach (var item in uiPageArray)
 {
 item.transform.GetChild(1).GetComponent<Text>().text = headPageIndex.ToString();

 headPageIndex++;
 }
 }
 else
 {
 for (int i = 0; i < maxPageIndex; i++)
 {
 uiPageArray[i].transform.GetChild(1).GetComponent<Text>().text = headPageIndex.ToString();

 headPageIndex++;
 }
 }
 }

 /// <summary>
 /// 在UIPage上更新
 /// </summary>
 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>().text = "1";
 }
 else if (i == 1)
 {
  uiPageArray[i].transform.GetChild(1).GetComponent<Text>().text = "...";
 }
 else
 {
  uiPageArray[i].transform.GetChild(1).GetComponent<Text>().text = count.ToString();
  count--;
 }
 }
 }
 else
 {
 int count = 1;
 for (int i = 0; i < maxPageIndex; i++)
 {
 uiPageArray[i].transform.GetChild(1).GetComponent<Text>().text = count.ToString();
 count++;
 }
 }
 }

 /// <summary>
 /// 在UIPage中间更新
 /// </summary>
 public void UpdateUIPageFromMiddle(string number)
 {
 int count = int.Parse(number);
 for (int i = 0; i < uiPageArray.Length; i++)
 {
 if (i == 0)
 {
 uiPageArray[i].transform.GetChild(1).GetComponent<Text>().text = "1";
 }
 else if (i == 1 || i == uiPageArray.Length - 2)
 {
 uiPageArray[i].transform.GetChild(1).GetComponent<Text>().text = "...";
 }
 else if (i == uiPageArray.Length - 1)
 {
 uiPageArray[i].transform.GetChild(1).GetComponent<Text>().text = maxPageIndex.ToString();
 }
 else
 {
 uiPageArray[i].transform.GetChild(1).GetComponent<Text>().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);
 }

 /// <summary>
 /// 按文本内容查询
 /// </summary>
 /// <param name="text"></param>
 public UIPage FindUIPageWithText(string text)
 {
 foreach (var item in uiPageArray)
 {
 if (item.transform.GetChild(1).GetComponent<Text>().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;
 }

 /// <summary>
 /// 页面跳转
 /// </summary>
 public void JumpPage()//这里用于输入框回车事件
 {
 //获取输入信息
 string number = GameObject.FindGameObjectWithTag("PageInputField").GetComponent<InputField>().text;
 if (string.IsNullOrEmpty(number))
 {
 return;
 }

 //查询前面几个ui(点击的是1-4)
 if (int.Parse(number) < PageGrid.GetInstance.uiPageArray.Length - 1)
 {
 PageGrid.GetInstance.UpadateUIPageFromHead();

 UIPage uiPage = PageGrid.GetInstance.FindUIPageWithText(number);
 if (uiPage)
 {
 GameObject obj = uiPage.gameObject;

 PageGrid.GetInstance.ActivatUIPageImage(obj);
 }
 }
 //查询最后几个(点击的是最后4个)
 else 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);
 }
 }
 }

 /// <summary>
 /// 跳转
 /// </summary>
 /// <param name="str"></param>
 public void JumpPage(string str)
 {
 //获取输入信息
 string number = str;

 //查询前面几个ui(点击的是1-4)
 if (int.Parse(number) < PageGrid.GetInstance.uiPageArray.Length - 1)
 {
 PageGrid.GetInstance.UpadateUIPageFromHead();

 UIPage uiPage = PageGrid.GetInstance.FindUIPageWithText(number);
 if (uiPage)
 {
 GameObject obj = uiPage.gameObject;

 PageGrid.GetInstance.ActivatUIPageImage(obj);
 }
 }
 //查询最后几个(点击的是最后4个)
 else 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);
 }
 }
 }

 /// <summary>
 /// 页面选择按钮
 /// </summary>
 /// <param name="selection">(向左:-1)( 向右:1)</param>
 public void OnBtnRight(string selection)
 {
 UIPage uiPage = PageGrid.GetInstance.FindUIPageWithImage();
 if (uiPage)
 {
 //当前页面是最后一页或者是第一页
 if (int.Parse(uiPage.transform.GetChild(1).GetComponent<Text>().text) == maxPageIndex && selection == "1" || int.Parse(uiPage.transform.GetChild(1).GetComponent<Text>().text) == 1 && selection == "-1")
 {
 return;
 }
 else
 {
 //跳转页面
 JumpPage((int.Parse(uiPage.transform.GetChild(1).GetComponent<Text>().text) + int.Parse(selection)).ToString());
 }
 }
 }
}

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

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

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

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

(0)

相关推荐

  • unity实现翻页按钮功能

    本文实例为大家分享了unity实现翻页按钮功能的具体代码,供大家参考,具体内容如下 效果图: UI子父级关系: 代码中也都有加入注释,有不懂可私信我.脚本中用到了对象池,我没有上传,可根据自己需求做相应变动. 脚本:PageBtnPanelC using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; using UnityEngine.UI; /

  • Unity3D实现列表分页效果

    本文实例为大家分享了Unity3D实现列表分页效果的具体代码,供大家参考,具体内容如下 using System.Collections.Generic; using UnityEngine; public class Page : MonoBehaviour { public List<string> Tips = new List<string>(); public Texture2D DetailImg1; public Texture2D DetailImg2; priva

  • Unity3D实现分页系统

    在有些情况下,有很多列表不能一次性显示完整,需要对其进行分页处理 博主自己也写了一个分页系统,在这里记录下来,方便以后直接拿来使用 这篇文章Demo也将上传给大家下载参考:点击打开链接 先展示下效果图: ·效果图一 ·效果图二 总的来说,要考虑到的逻辑情况还是有点的 工程目录结构如下图: 在每个UIPage下有一个Image框,用来编辑当前是那一页,默认activate=false 整个思路是当点击UIPage获取里面的页数数值,根据这个数值刷新下UIPage的Text值 在确定哪个UIPage

  • ASP正则函数替换分页后的参数

    Function DRexPage(Str)     Dim RegEx     If IsNull(Str) Or Str="" Then Exit Function    Set RegEx=New RegExp         RegEx.IgnoreCase=True        RegEx.pattern="(\&)?page=(\d)+"        DRexPage=regEx.replace(Str,"")  '(St

  • C#遍历系统进程的方法

    本文实例讲述了C#遍历系统进程的方法.分享给大家供大家参考.具体实现方法如下: 建立一个listBox将进程名称遍历进去 this.listBox1.Items.Clear(); Process[] MyProcesses=Process.GetProcesses(); foreach(Process MyProcess in MyProcesses) { this.listBox1.Items.Add(MyProcess.ProcessName); } this.listBox1.Select

  • 全面解读PHP的人气开发框架Laravel

    Laravel的主要技术特点: 1.Bundle是Laravel的扩展包组织形式或称呼.Laravel的扩展包仓库已经相当成熟了,可以很容易的帮你把扩展包(bundle)安装到你的应用中.你可以选择下载一个扩展包(bundle)然后拷贝到bundles目录,或者通过命令行工具"Artisan"自动安装. 2.在Laravel中已经具有了一套高级的PHP ActiveRecord实现 -- Eloquent ORM.它能方便的将"约束(constraints)"应用到

  • 详解在Vue.js编写更好的v-for循环的6种技巧

    在VueJS中,v-for循环是每个项目都会使用的东西,它允许您在模板代码中编写for循环. 在最基本的用法中,它们的用法如下. <ul> <li v-for='product in products'> {{ product.name }} </li> </ul> 但是,在本文中,我将介绍六种方法来使你的 v-for 代码更加精确,可预测和强大. 让我们开始吧. 1.始终在v-for循环中使用key 首先,我们将讨论大多数Vue开发人员已经知道的常见最佳做

  • 编写v-for循环的技巧汇总

    在 Vue.js 中,v-for 循环是每个项目都会使用的东西,它允许您在模板代码中编写 for 循环. 在最基本的用法中,它们的用法如下. <ul> <li v-for="product in products"> {{ product.name }} </li> </ul> 但是,在本文中,我将介绍六种方法来使你的 v-for 代码更加精确,可预测和强大. 让我们开始吧. 1.始终在 v-for 循环中使用 key 首先,我们将讨论大

  • 编写 Vue v-for 循环的 7 种方式

    目录 1. 始终在v-for循环中使用key 2. 在一定范围内使用v-for循环 3. 避免在循环中使用v-if 4. 使用computed属性或方法 5. 或者在循环外包一层元素 6. 访问循环中的索引 7. 迭代对象 这在碰到诸如以下情况时特别好用: 渲染数组或列表 遍历对象属性 在Vue中v-for循环最基本的用法是这样的: <ul> <li v-for='product in products'> {{ product.name }} </li> </u

  • .NET Core跨平台资源监控工具CZGL.SystemInfo用法

    目录 简介 dotnet tool 体验 CZGL.SystemInfo SystemPlatformInfo ProcessInfo 内存监控 NetworkInfo DiskInfo Linux 直接使用 简介 CZGL.SystemInfo 是一个支持 Windows 和 Linux 等平台的能够获取机器硬件信息.采集机器资源信息.监控进程资源的库. 在不引入额外依赖的情况下,使用 .NET Runtime 本身的 API,或通过计算获得信息,提供高性能的计算方式以及缓存,提高性能,还提供

  • Vue指令之v-for的使用说明

    目录 Vue v-for的使用 1.迭代普通数组 2.迭代对象数组 3.迭代对象 4.迭代数字 v-for的最佳使用技巧 1.始终在v-for循环中使用key 2.不要在循环中使用v-if 3.使用计算属性或方法来处理数据后再做数据遍历 4.在一个范围内循环 5.在循环中访问项目的索引 6.遍历一个对象 Vue v-for的使用 1.迭代普通数组 在data中定义普通数组 data:{       list:[1,2,3,4,5,6] } 在html中使用 v-for 指令渲染 <p v-for

  • Unity游戏开发之2048游戏的实现

    目录 一.前言 二.游戏开发知识储备 2-1技术栈 三.休闲类游戏<2048>开发实战 3-1玩法概述 3-2实现分析 3-3搭建场景 3-4实现代码 一.前言 写今天这篇文章的缘由,其实是来自于前段时间和粉丝的一个聊天,最近他打算参加游戏创作大赛,问我需要准备学习什么知识,以及参加比赛的注意事项一类: 我相信因为热爱游戏而前来投身于U3D学习的粉丝想来不是少数,兴趣可以驱动学习,在完善自己心爱游戏的过程中,要不断的去学习,不断的提高自己. 而参与游戏设计比赛,更是提高自身技术实力.增长眼界见

随机推荐