123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- using UnityEngine;
- using UnityEngine.UI;
-
- public class Pagination : MonoBehaviour
- {
- public int totalPages; // 总页数
- public int currentPage; // 总页数
- public int visiblePages; // 可见页码数量
- public Button firstPageButton;
- public Button previousPageButton;
- public Button nextPageButton;
- public Button lastPageButton;
- public GameObject pageButtonPrefab;
- public Transform pageButtonsContainer;
-
- void Start()
-
- {
- UpdatePagination();
- }
-
- public void GoToPage(int page)
- {
- // 分页逻辑...
- }
-
- public void UpdatePagination()
- {
- int startPage = 1;
- int endPage = totalPages;
-
- if (totalPages > visiblePages)
- {
- startPage = Mathf.Max(1, currentPage - visiblePages / 2);
- endPage = Mathf.Min(totalPages, startPage + visiblePages - 1);
-
- if (endPage - startPage < visiblePages - 1)
- {
- startPage = endPage - visiblePages + 1;
- }
-
- if (startPage > 1)
- {
- firstPageButton.interactable = true;
- previousPageButton.interactable = true;
- }
- else
- {
- firstPageButton.interactable = false;
- previousPageButton.interactable = false;
- }
-
- if (endPage < totalPages)
- {
- lastPageButton.interactable = true;
- nextPageButton.interactable = true;
- }
- else
- {
- lastPageButton.interactable = false;
- nextPageButton.interactable = false;
- }
-
- if (startPage > 3)
- {
- pageButtonsContainer.GetChild(0).GetComponent<Text>().text = "1";
- pageButtonsContainer.GetChild(1).GetComponent<Text>().text = "...";
- }
- if (endPage < totalPages - 1)
- {
- pageButtonsContainer.GetChild(pageButtonsContainer.childCount - 1).GetComponent<Text>().text = totalPages.ToString();
- pageButtonsContainer.GetChild(pageButtonsContainer.childCount - 2).GetComponent<Text>().text = "...";
- }
- }
-
- // 清除旧的页码按钮
- for (int i = 0; i < pageButtonsContainer.childCount; )
- {
- Destroy(pageButtonsContainer.GetChild(i).gameObject);
- }
-
- // 创建新的页码按钮
- for (int i = startPage; i <= endPage; i++)
- {
- Button pageButton = Instantiate(pageButtonPrefab, pageButtonsContainer).GetComponent<Button>();
- pageButton.GetComponent<Text>().text = i.ToString();
- pageButton.onClick.AddListener(() => GoToPage(i));
- }
- }
- }
|