156 lines
4.4 KiB
C#
156 lines
4.4 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace Assets.Scripts.UI.Control
|
|
{
|
|
[RequireComponent(typeof(ScrollRect))]
|
|
public class PFUIPageHelper : PFUIComponentBase
|
|
{
|
|
public Transform content;
|
|
public GameObject moreBtnPrefab;
|
|
public GameObject preBtnPrefab;
|
|
public GameObject nextBtnPrefab;
|
|
public GameObject btnPrefab;
|
|
public int PageIndex { get; set; }
|
|
public int PageSize { get; set; } = 6;
|
|
public int Total { get; set; }
|
|
|
|
public int _pageMaxTotal = 9;
|
|
|
|
public int _pageSpace = 4;
|
|
|
|
private Action<int> CallBack { get; set; }
|
|
|
|
public void Register(Action<int> refresh)
|
|
{
|
|
CallBack = refresh;
|
|
}
|
|
public void Build()
|
|
{
|
|
Utils.DestroyChildren(content);
|
|
|
|
RenderPreviousButton();
|
|
|
|
for (int i = 0; i < Total; i++)
|
|
{
|
|
if (PageIndex - 0 >= 4 && i < PageIndex)
|
|
{
|
|
if (i == 0 || PageIndex -1 == i || PageIndex -2 == i)
|
|
{
|
|
RenderPageButton(i);
|
|
}
|
|
if (i == 1)
|
|
{
|
|
RenderMoreButton();
|
|
}
|
|
continue;
|
|
}
|
|
|
|
if (Total -PageIndex-1 >= 4 && i >= PageIndex)
|
|
{
|
|
if (i == Total-1 || PageIndex == i || PageIndex + 1 == i || PageIndex + 2 == i)
|
|
{
|
|
RenderPageButton(i);
|
|
}
|
|
if (i == Total - 2)
|
|
{
|
|
RenderMoreButton();
|
|
}
|
|
continue;
|
|
}
|
|
RenderPageButton(i);
|
|
}
|
|
RenderNextButton();
|
|
}
|
|
private void RenderMoreButton()
|
|
{
|
|
var moreBtn = Instantiate(moreBtnPrefab, content);
|
|
moreBtn.SetActive(true);
|
|
}
|
|
private void RenderPageButton(int pageIndex)
|
|
{
|
|
var btn = Instantiate(btnPrefab, content);
|
|
var script = btn.GetComponent<PFUIPageButton>();
|
|
script.SetPageIndex(pageIndex);
|
|
|
|
if (pageIndex == PageIndex)
|
|
{
|
|
script.ShowLight();
|
|
}
|
|
else
|
|
{
|
|
script.ShowShadow();
|
|
}
|
|
|
|
UIManager.AddEvent(btn, UnityEngine.EventSystems.EventTriggerType.PointerClick, (e) =>
|
|
{
|
|
Redirect(pageIndex);
|
|
});
|
|
}
|
|
private void RenderNextButton()
|
|
{
|
|
var nextBtn = Instantiate(nextBtnPrefab, content);
|
|
nextBtn.SetActive(true);
|
|
|
|
UIManager.AddEvent(nextBtn, UnityEngine.EventSystems.EventTriggerType.PointerClick, (e) =>
|
|
{
|
|
Next();
|
|
});
|
|
}
|
|
private void RenderPreviousButton()
|
|
{
|
|
var preBtn = Instantiate(preBtnPrefab, content);
|
|
preBtn.SetActive(true);
|
|
|
|
UIManager.AddEvent(preBtn, UnityEngine.EventSystems.EventTriggerType.PointerClick, (e) =>
|
|
{
|
|
Previous();
|
|
});
|
|
}
|
|
//上一页
|
|
private void Previous()
|
|
{
|
|
var newIndex = PageIndex - 1;
|
|
var index = newIndex < 0 ? 0 : newIndex;
|
|
if (index != PageIndex)
|
|
{
|
|
PageIndex = index;
|
|
CallBack?.Invoke(PageIndex);
|
|
Build();
|
|
}
|
|
}
|
|
//下一页
|
|
private void Next()
|
|
{
|
|
var newIndex = PageIndex + 1;
|
|
var index = newIndex >= Total ? Total-1 : newIndex;
|
|
if (index != PageIndex)
|
|
{
|
|
PageIndex = index;
|
|
CallBack?.Invoke(PageIndex);
|
|
Build();
|
|
}
|
|
}
|
|
//指定到第几页
|
|
private void Redirect(int index)
|
|
{
|
|
var list = FindObjectsOfType<PFUIPageButton>();
|
|
foreach (var item in list)
|
|
{
|
|
if (item.PageIndex == index)
|
|
{
|
|
item.ShowLight();
|
|
}
|
|
else
|
|
{
|
|
item.ShowShadow();
|
|
}
|
|
}
|
|
PageIndex = index;
|
|
CallBack?.Invoke(PageIndex);
|
|
Build();
|
|
}
|
|
}
|
|
}
|