144 lines
3.3 KiB
C#
144 lines
3.3 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class UI_ChooesPanel : BasePanel
|
|
{
|
|
public Image targetimage;
|
|
public Button prevButton; // 上一页按钮
|
|
public Button nextButton; // 下一页按钮
|
|
|
|
public int currentPage = 1; // 当前页码
|
|
public List<GameObject> Pages; // 页面列表
|
|
public int visiblePages = 5; // 同时显示的页面数量
|
|
private int startIndex = 0; // 当前显示的第一个页面的索引
|
|
|
|
protected override void Awake()
|
|
{
|
|
base.Awake();
|
|
OnInit();
|
|
}
|
|
|
|
public void OnInit()
|
|
{
|
|
targetimage = GetControl<Image>("实验对应图片");
|
|
prevButton = GetControl<Button>("上一页");
|
|
nextButton = GetControl<Button>("下一页");
|
|
|
|
// 初始化显示前5个页面
|
|
UpdatePageDisplay();
|
|
}
|
|
|
|
public override void ShowMe()
|
|
{
|
|
base.ShowMe();
|
|
ShowFirstPage();
|
|
}
|
|
|
|
public override void HideMe()
|
|
{
|
|
base.HideMe();
|
|
HideAllPages();
|
|
}
|
|
|
|
protected override void OnClick(string btnPath)
|
|
{
|
|
base.OnClick(btnPath);
|
|
switch (btnPath)
|
|
{
|
|
case "上一页":
|
|
PrevPage();
|
|
break;
|
|
case "下一页":
|
|
NextPage();
|
|
break;
|
|
}
|
|
}
|
|
|
|
// 隐藏所有页面
|
|
private void HideAllPages()
|
|
{
|
|
foreach (var page in Pages)
|
|
{
|
|
page.SetActive(false);
|
|
}
|
|
}
|
|
|
|
// 显示第一组页面
|
|
public void ShowFirstPage()
|
|
{
|
|
startIndex = 0;
|
|
currentPage = 1;
|
|
UpdatePageDisplay();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 上一页 - 向左滑动
|
|
/// </summary>
|
|
public void PrevPage()
|
|
{
|
|
if (startIndex > 0)
|
|
{
|
|
startIndex--;
|
|
UpdatePageDisplay();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 下一页 - 向右滑动
|
|
/// </summary>
|
|
public void NextPage()
|
|
{
|
|
if (startIndex + visiblePages < Pages.Count)
|
|
{
|
|
startIndex++;
|
|
UpdatePageDisplay();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新页面显示
|
|
/// </summary>
|
|
public void UpdatePageDisplay()
|
|
{
|
|
// 隐藏所有页面
|
|
HideAllPages();
|
|
|
|
// 显示当前可见范围内的页面
|
|
int endIndex = Mathf.Min(startIndex + visiblePages - 1, Pages.Count - 1);
|
|
|
|
for (int i = startIndex; i <= endIndex; i++)
|
|
{
|
|
Pages[i].SetActive(true);
|
|
}
|
|
|
|
// 更新按钮状态
|
|
UpdateButtonState();
|
|
|
|
// 更新当前页码(显示的第一个页面)
|
|
currentPage = startIndex + 1;
|
|
|
|
Debug.Log($"显示页面 {startIndex + 1} 到 {endIndex + 1}, 当前页: {currentPage}");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新按钮的显示状态
|
|
/// </summary>
|
|
private void UpdateButtonState()
|
|
{
|
|
// 第一组页面时隐藏上一页按钮
|
|
prevButton.gameObject.SetActive(startIndex > 0);
|
|
|
|
// 最后一组页面时隐藏下一页按钮
|
|
int maxStartIndex = Mathf.Max(0, Pages.Count - visiblePages);
|
|
nextButton.gameObject.SetActive(startIndex < maxStartIndex);
|
|
|
|
// 如果总页面数小于等于可见页面数,两个按钮都隐藏
|
|
if (Pages.Count <= visiblePages)
|
|
{
|
|
prevButton.gameObject.SetActive(false);
|
|
nextButton.gameObject.SetActive(false);
|
|
}
|
|
}
|
|
} |