powerfun-unity/Assets/Scripts/UI/Control/PFUIPageHelper.cs
2022-05-10 19:24:07 +08:00

72 lines
2.0 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 preBtn;
public GameObject nextBtn;
public GameObject btnPrefab;
public int PageIndex { get; set; }
public int PageSize { get; set; } = 6;
public int Total { get; set; }
private Action<int> CallBack { get; set; }
public void Register(Action<int> refresh)
{
CallBack = refresh;
UIManager.AddEvent(preBtn, UnityEngine.EventSystems.EventTriggerType.PointerClick, (e) =>
{
Previous();
});
UIManager.AddEvent(nextBtn, UnityEngine.EventSystems.EventTriggerType.PointerClick, (e) =>
{
Next();
});
}
public void Build()
{
Utils.DestroyChildren(content);
for (int i = 1; i <= Total; i++)
{
var g = Instantiate(btnPrefab, content);
g.transform.Find("Text").GetComponent<Text>().text = i.ToString();
}
}
//上一页
private void Previous()
{
var newIndex = PageIndex - 1;
var index = newIndex < 0 ? 0 : newIndex;
if (index != PageIndex)
{
PageIndex = index;
CallBack?.Invoke(PageIndex);
}
}
//下一页
private void Next()
{
var newIndex = PageIndex + 1;
var index = newIndex >= Total ? Total-1 : newIndex;
if (index != PageIndex)
{
PageIndex = index;
CallBack?.Invoke(PageIndex);
}
}
//指定到第几页
private void Redirect(int index)
{
PageIndex = index;
CallBack?.Invoke(PageIndex);
}
}
}