powerfun-unity/Assets/Scripts/UI/Control/PFUIPageHelper.cs

156 lines
4.4 KiB
C#
Raw Normal View History

2022-05-10 19:24:07 +08:00
using System;
using UnityEngine;
using UnityEngine.UI;
namespace Assets.Scripts.UI.Control
{
[RequireComponent(typeof(ScrollRect))]
public class PFUIPageHelper : PFUIComponentBase
{
public Transform content;
2022-05-13 09:28:58 +08:00
public GameObject moreBtnPrefab;
public GameObject preBtnPrefab;
public GameObject nextBtnPrefab;
2022-05-10 19:24:07 +08:00
public GameObject btnPrefab;
public int PageIndex { get; set; }
public int PageSize { get; set; } = 6;
public int Total { get; set; }
2022-05-13 09:28:58 +08:00
public int _pageMaxTotal = 9;
public int _pageSpace = 4;
2022-05-10 19:24:07 +08:00
private Action<int> CallBack { get; set; }
public void Register(Action<int> refresh)
{
CallBack = refresh;
2022-05-13 09:28:58 +08:00
}
public void Build()
{
Utils.DestroyChildren(content);
2022-05-10 19:24:07 +08:00
2022-05-13 09:28:58 +08:00
RenderPreviousButton();
for (int i = 0; i < Total; i++)
2022-05-10 19:24:07 +08:00
{
2022-05-13 09:28:58 +08:00
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);
2022-05-10 19:24:07 +08:00
});
2022-05-13 09:28:58 +08:00
}
private void RenderNextButton()
{
var nextBtn = Instantiate(nextBtnPrefab, content);
nextBtn.SetActive(true);
2022-05-10 19:24:07 +08:00
UIManager.AddEvent(nextBtn, UnityEngine.EventSystems.EventTriggerType.PointerClick, (e) =>
{
Next();
});
}
2022-05-13 09:28:58 +08:00
private void RenderPreviousButton()
2022-05-10 19:24:07 +08:00
{
2022-05-13 09:28:58 +08:00
var preBtn = Instantiate(preBtnPrefab, content);
preBtn.SetActive(true);
UIManager.AddEvent(preBtn, UnityEngine.EventSystems.EventTriggerType.PointerClick, (e) =>
2022-05-10 19:24:07 +08:00
{
2022-05-13 09:28:58 +08:00
Previous();
});
2022-05-10 19:24:07 +08:00
}
//上一页
private void Previous()
{
var newIndex = PageIndex - 1;
var index = newIndex < 0 ? 0 : newIndex;
if (index != PageIndex)
{
PageIndex = index;
CallBack?.Invoke(PageIndex);
2022-05-13 09:28:58 +08:00
Build();
2022-05-10 19:24:07 +08:00
}
}
//下一页
private void Next()
{
var newIndex = PageIndex + 1;
var index = newIndex >= Total ? Total-1 : newIndex;
if (index != PageIndex)
{
PageIndex = index;
CallBack?.Invoke(PageIndex);
2022-05-13 09:28:58 +08:00
Build();
2022-05-10 19:24:07 +08:00
}
}
//指定到第几页
private void Redirect(int index)
{
2022-05-13 09:28:58 +08:00
var list = FindObjectsOfType<PFUIPageButton>();
foreach (var item in list)
{
if (item.PageIndex == index)
{
item.ShowLight();
}
else
{
item.ShowShadow();
}
}
2022-05-10 19:24:07 +08:00
PageIndex = index;
CallBack?.Invoke(PageIndex);
2022-05-13 09:28:58 +08:00
Build();
2022-05-10 19:24:07 +08:00
}
}
}