欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Unity3D實(shí)現(xiàn)分頁(yè)系統(tǒng)

 更新時(shí)間:2020年04月15日 10:52:03   作者:即步  
這篇文章主要為大家詳細(xì)介紹了Unity3D實(shí)現(xiàn)分頁(yè)系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

在有些情況下,有很多列表不能一次性顯示完整,需要對(duì)其進(jìn)行分頁(yè)處理

博主自己也寫了一個(gè)分頁(yè)系統(tǒng),在這里記錄下來(lái),方便以后直接拿來(lái)使用

這篇文章Demo也將上傳給大家下載參考:點(diǎn)擊打開鏈接

先展示下效果圖:

·效果圖一

·效果圖二

總的來(lái)說(shuō),要考慮到的邏輯情況還是有點(diǎn)的

工程目錄結(jié)構(gòu)如下圖:

在每個(gè)UIPage下有一個(gè)Image框,用來(lái)編輯當(dāng)前是那一頁(yè),默認(rèn)activate=false

整個(gè)思路是當(dāng)點(diǎn)擊UIPage獲取里面的頁(yè)數(shù)數(shù)值,根據(jù)這個(gè)數(shù)值刷新下UIPage的Text值

在確定哪個(gè)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;
 }
 }
 
 
 //點(diǎn)擊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;
 
 //如果點(diǎn)擊的是前面幾個(gè)ui(點(diǎn)擊的是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);
 }
 }
 //點(diǎn)擊最后幾個(gè)(點(diǎn)擊的是最后4個(gè))
 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);
 
 /*由于數(shù)字向后移動(dòng),故image顯示位置不需要改變*/
 }
 }
}

在做完了UIPage的點(diǎn)擊事件后,需要實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)(左右按鈕的點(diǎn)擊實(shí)際也是一個(gè)跳轉(zhuǎn))

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 
/// <summary>
/// 管理UIPage
/// </summary>
public class PageGrid : MonoBehaviour
{
 //在初始化時(shí)最大的頁(yè)數(shù)
 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()
 {
 //獲取其子節(jié)點(diǎn)UIPage組件
 uiPageArray = this.GetComponentsInChildren<UIPage>();
 
 //初始化子節(jié)點(diǎn)ui顯示
 UpadateUIPageFromHead();
 }
 
 /// <summary>
 /// 在UIPage上更新
 /// </summary>
 public void UpadateUIPageFromHead()
 {
 //從一開始計(jì)數(shù)
 int headPageIndex = 1;
 
 int n_pageHeadIndex = headPageIndex;
 
 //頁(yè)數(shù)大于UIPage數(shù)(大于6)
 if (maxPageIndex > uiPageArray.Length)
 {
 foreach (var item in uiPageArray)
 {
 //倒數(shù)第二個(gè)
 if (headPageIndex - n_pageHeadIndex == uiPageArray.Length - 2)
 {
  item.transform.GetChild(1).GetComponent<Text>().text = "...";
 }
 //倒數(shù)第一個(gè)
 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++; 
 }
 }
 //頁(yè)數(shù)等于UIPage數(shù)
 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()
 {
 //頁(yè)數(shù)大于UIPage數(shù)(大于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++;
 }
 }
 }
 
 
 
 //需要和服務(wù)器交互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>
 /// 按文本內(nèi)容查詢
 /// </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>
 /// 頁(yè)面跳轉(zhuǎn)
 /// </summary>
 public void JumpPage()//這里用于輸入框回車事件
 {
 //獲取輸入信息
 string number = GameObject.FindGameObjectWithTag("PageInputField").GetComponent<InputField>().text;
 if (string.IsNullOrEmpty(number))
 {
 return;
 }
 
 
 //查詢前面幾個(gè)ui(點(diǎn)擊的是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);
 }
 }
 //查詢最后幾個(gè)(點(diǎn)擊的是最后4個(gè))
 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>
 /// 跳轉(zhuǎn)
 /// </summary>
 /// <param name="str"></param>
 public void JumpPage(string str)
 {
 //獲取輸入信息
 string number = str;
 
 
 //查詢前面幾個(gè)ui(點(diǎn)擊的是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);
 }
 }
 //查詢最后幾個(gè)(點(diǎn)擊的是最后4個(gè))
 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>
 /// 頁(yè)面選擇按鈕
 /// </summary>
 /// <param name="selection">(向左:-1)( 向右:1)</param>
 public void OnBtnRight(string selection)
 {
 UIPage uiPage = PageGrid.GetInstance.FindUIPageWithImage();
 if (uiPage)
 {
 //當(dāng)前頁(yè)面是最后一頁(yè)或者是第一頁(yè)
 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
 {
 //跳轉(zhuǎn)頁(yè)面
 JumpPage((int.Parse(uiPage.transform.GetChild(1).GetComponent<Text>().text) + int.Parse(selection)).ToString());
 }
 }
 }
}

將該腳本掛載到父節(jié)點(diǎn)pageGrid上

最后需要將條形框回車事件綁定上去

這樣一個(gè)完成簡(jiǎn)單分頁(yè)系統(tǒng)

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C#中HttpWebRequest的用法詳解

    C#中HttpWebRequest的用法詳解

    這篇文章主要介紹了C#中HttpWebRequest的用法,以實(shí)例的形式詳細(xì)敘述了HttpWebRequest類中GET與POST的用法,非常具有參考借鑒價(jià)值,需要的朋友可以參考下
    2014-11-11
  • 如何隨機(jī)選取一個(gè)機(jī)器的ip

    如何隨機(jī)選取一個(gè)機(jī)器的ip

    此方法可用于當(dāng)需要負(fù)載均衡時(shí),即想選擇多臺(tái)機(jī)器來(lái)處理程序執(zhí)行。 隨機(jī)的選擇一臺(tái)機(jī)器來(lái)處理。
    2013-03-03
  • Unity?UGUI的PointerEventData的介紹及使用

    Unity?UGUI的PointerEventData的介紹及使用

    這篇文章主要為大家介紹了Unity?UGUI的PointerEventData的介紹及使用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • C#算法之各位相加

    C#算法之各位相加

    這篇文章介紹了C#算法之各位相加,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-01-01
  • 詳解WPF中值轉(zhuǎn)換器的使用方法

    詳解WPF中值轉(zhuǎn)換器的使用方法

    在WPF(Windows Presentation Foundation)中,值轉(zhuǎn)換器(Value Converter)是一種機(jī)制,允許你在綁定時(shí)轉(zhuǎn)換綁定源和綁定目標(biāo)之間的值,本文給大家介紹了WPF中值轉(zhuǎn)換器的使用方法,需要的朋友可以參考下
    2024-02-02
  • C#調(diào)用帶結(jié)構(gòu)體指針Dll的方法

    C#調(diào)用帶結(jié)構(gòu)體指針Dll的方法

    在C#到底該如何安全的調(diào)用這樣的DLL接口函數(shù)呢?本文將詳細(xì)介紹如何調(diào)用各種參數(shù)的方法,對(duì)C#結(jié)構(gòu)體指針DLL相關(guān)知識(shí)感興趣的朋友一起看看吧
    2021-07-07
  • WPF開發(fā)技巧之花式控件功能擴(kuò)展詳解

    WPF開發(fā)技巧之花式控件功能擴(kuò)展詳解

    這篇文章主要給大家介紹了關(guān)于WPF日常開發(fā)之花式控件功能擴(kuò)展的相關(guān)資料,通過(guò)文中這個(gè)例子,我們可以對(duì)WPF的掌握會(huì)更深刻,需要的朋友可以參考下
    2021-07-07
  • C#集合遍歷時(shí)刪除和增加元素的方法

    C#集合遍歷時(shí)刪除和增加元素的方法

    這篇文章主要介紹了C#集合遍歷時(shí)刪除和增加元素的方法,結(jié)合實(shí)例形式分析了C#針對(duì)集合元素的遍歷、添加與刪除等操作實(shí)現(xiàn)方法與注意事項(xiàng),需要的朋友可以參考下
    2016-06-06
  • C# 抓圖服務(wù)的實(shí)現(xiàn)

    C# 抓圖服務(wù)的實(shí)現(xiàn)

    這篇文章主要介紹了C# 抓圖服務(wù)的實(shí)現(xiàn),幫助大家更好的利用c#處理窗口,進(jìn)行截圖,感興趣的朋友可以了解下
    2021-01-01
  • C# 異步返回類型的幾種方式

    C# 異步返回類型的幾種方式

    異步編程已經(jīng)成為一種重要的編程范式,本文主要介紹了C#異步返回類型的幾種方式,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-03-03

最新評(píng)論