Unity3D实现分页系统

作者:即步 时间:2022-06-28 22:53:37 

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

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

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

先展示下效果图:

Unity3D实现分页系统

·效果图一

Unity3D实现分页系统

·效果图二

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

工程目录结构如下图:

Unity3D实现分页系统

在每个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上

Unity3D实现分页系统

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

Unity3D实现分页系统

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

来源:https://blog.csdn.net/qq_33747722/article/details/74924458

标签:Unity3D,分页
0
投稿

猜你喜欢

  • Java SpringBoot实现带界面的代码生成器详解

    2023-09-28 11:54:14
  • Unity 通过LineRenderer绘制两点之间的直线操作

    2021-08-04 04:15:08
  • C# WebApi 路由机制剖析

    2022-01-23 05:20:16
  • Java 栈与队列实战真题训练

    2021-06-11 01:46:08
  • C#结束进程及子进程

    2021-06-22 15:18:10
  • AndroidStudio接入Unity工程并实现相互跳转的示例代码

    2023-08-06 23:34:51
  • Java基础知识之CharArrayReader流的使用

    2023-02-12 10:40:29
  • C#中TreeView节点的自定义绘制方法

    2023-04-01 10:37:19
  • 解决Spring Security中AuthenticationEntryPoint不生效相关问题

    2022-11-29 06:53:09
  • RocketMQ之NameServer架构设计及启动关闭流程源码分析

    2021-07-30 20:43:56
  • 基于Java中两种jersey文件上传方式

    2022-03-02 10:47:29
  • Java创建树形结构算法实例代码

    2021-11-21 22:24:01
  • java 过滤器filter防sql注入的实现代码

    2023-08-30 09:56:40
  • C#判断数据类型的简单示例代码

    2023-09-22 22:22:48
  • 基数排序简介及Java语言实现

    2021-10-06 01:15:13
  • Java设计模式之抽象工厂模式浅析讲解

    2022-08-08 18:26:47
  • Android实现下拉菜单Spinner效果

    2022-03-21 07:27:07
  • Java编程计算兔子生兔子的问题

    2023-08-01 15:24:37
  • @JsonFormat处理LocalDateTime失效的问题

    2023-07-22 18:41:13
  • SpringBoot消息国际化配置实现过程解析

    2023-05-16 01:19:22
  • asp之家 软件编程 m.aspxhome.com