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 = "1"; pageButtonsContainer.GetChild(1).GetComponent().text = "..."; } if (endPage < totalPages - 1) { pageButtonsContainer.GetChild(pageButtonsContainer.childCount - 1).GetComponent().text = totalPages.ToString(); pageButtonsContainer.GetChild(pageButtonsContainer.childCount - 2).GetComponent().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