| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.UI; |
| | 5 | |
|
| | 6 | | public class UIPageSelector : MonoBehaviour |
| | 7 | | { |
| | 8 | | public event Action<int> OnValueChanged; |
| | 9 | |
|
| | 10 | | [SerializeField] private Button previousButton; |
| | 11 | | [SerializeField] private Button nextButton; |
| | 12 | | [SerializeField] private UIPageButton pageButtonPrefab; |
| | 13 | | [SerializeField] private RectTransform pageButtonsParent; |
| | 14 | | [SerializeField] private RectTransform rectTransform; |
| | 15 | | [SerializeField] private bool limitedPages = false; |
| | 16 | | [SerializeField] private int maxVisiblePages; |
| 0 | 17 | | private List<UIPageButton> buttons = new List<UIPageButton>(); |
| | 18 | | private int totalPages; |
| | 19 | | private int currentPage; |
| | 20 | |
|
| | 21 | | private void Awake() |
| | 22 | | { |
| 0 | 23 | | previousButton.onClick.AddListener(OnPreviousButtonDown); |
| 0 | 24 | | nextButton.onClick.AddListener(OnNextButtonDown); |
| | 25 | |
|
| 0 | 26 | | gameObject.SetActive(false); |
| 0 | 27 | | } |
| | 28 | |
|
| | 29 | | private void OnNextButtonDown() |
| | 30 | | { |
| 0 | 31 | | currentPage = (currentPage + 1 ) % totalPages; |
| 0 | 32 | | UpdateButtonsStatus(); |
| 0 | 33 | | } |
| | 34 | |
|
| | 35 | | private void OnPreviousButtonDown() |
| | 36 | | { |
| 0 | 37 | | if (currentPage - 1 < 0) |
| | 38 | | { |
| 0 | 39 | | currentPage = totalPages - 1; |
| | 40 | | } |
| | 41 | | else |
| | 42 | | { |
| 0 | 43 | | currentPage = (currentPage - 1 ) % totalPages; |
| | 44 | | } |
| | 45 | |
|
| 0 | 46 | | UpdateButtonsStatus(); |
| 0 | 47 | | } |
| | 48 | |
|
| | 49 | | public void SelectPage(int pageNumber) |
| | 50 | | { |
| 0 | 51 | | currentPage = pageNumber; |
| 0 | 52 | | UpdateButtonsStatus(); |
| 0 | 53 | | } |
| | 54 | |
|
| | 55 | | public void Setup(int maxTotalPages, bool forceRebuild = false) |
| | 56 | | { |
| 0 | 57 | | if (maxTotalPages == this.totalPages && !forceRebuild) |
| | 58 | | { |
| 0 | 59 | | return; |
| | 60 | | } |
| | 61 | |
|
| 0 | 62 | | this.totalPages = maxTotalPages; |
| | 63 | |
|
| 0 | 64 | | currentPage = Mathf.Clamp(currentPage, 0, maxTotalPages-1); |
| | 65 | |
|
| 0 | 66 | | if (maxTotalPages <= 1) |
| | 67 | | { |
| 0 | 68 | | gameObject.SetActive(false); |
| 0 | 69 | | OnValueChanged?.Invoke(0); |
| 0 | 70 | | return; |
| | 71 | | } |
| | 72 | |
|
| 0 | 73 | | gameObject.SetActive(true); |
| | 74 | |
|
| 0 | 75 | | EnsureButtons(); |
| 0 | 76 | | UpdateButtonsStatus(false); |
| | 77 | |
|
| 0 | 78 | | LayoutRebuilder.ForceRebuildLayoutImmediate(pageButtonsParent); |
| 0 | 79 | | LayoutRebuilder.ForceRebuildLayoutImmediate(rectTransform); |
| 0 | 80 | | } |
| | 81 | |
|
| | 82 | | private void EnsureButtons() |
| | 83 | | { |
| 0 | 84 | | if (buttons.Count != totalPages) |
| | 85 | | { |
| 0 | 86 | | var diff = totalPages - buttons.Count; |
| | 87 | |
|
| 0 | 88 | | if (diff > 0) |
| | 89 | | { |
| 0 | 90 | | for (int i = 0; i < diff; i++) |
| | 91 | | { |
| 0 | 92 | | var instance = Instantiate(pageButtonPrefab, pageButtonsParent); |
| 0 | 93 | | buttons.Add(instance); |
| | 94 | | } |
| | 95 | | } |
| | 96 | | } |
| | 97 | |
|
| 0 | 98 | | for (int i = 0; i < buttons.Count; i++) |
| | 99 | | { |
| 0 | 100 | | UIPageButton uiPageButton = buttons[i]; |
| | 101 | |
|
| 0 | 102 | | if (i >= totalPages) |
| | 103 | | { |
| 0 | 104 | | uiPageButton.gameObject.SetActive(false); |
| | 105 | |
|
| 0 | 106 | | continue; |
| | 107 | | } |
| | 108 | |
|
| 0 | 109 | | uiPageButton.Initialize(i); |
| 0 | 110 | | uiPageButton.gameObject.SetActive(true); |
| 0 | 111 | | uiPageButton.OnPageClicked -= SelectPage; |
| 0 | 112 | | uiPageButton.OnPageClicked += SelectPage; |
| | 113 | | } |
| 0 | 114 | | } |
| | 115 | |
|
| | 116 | | private bool ShouldShowButton(int buttonIndex) |
| | 117 | | { |
| 0 | 118 | | if (buttonIndex >= totalPages) |
| 0 | 119 | | return false; |
| | 120 | |
|
| 0 | 121 | | if (currentPage+1 <= maxVisiblePages / 2) |
| | 122 | | { |
| 0 | 123 | | return buttonIndex < maxVisiblePages; |
| | 124 | | } |
| | 125 | | else |
| | 126 | | { |
| 0 | 127 | | return buttonIndex < currentPage+1 + (maxVisiblePages / 2) && buttonIndex+1 > currentPage - (maxVisiblePages |
| | 128 | | } |
| | 129 | | } |
| | 130 | |
|
| | 131 | | private void UpdateButtonsStatus(bool notifyEvent = true) |
| | 132 | | { |
| 0 | 133 | | UpdateToggleStatus(); |
| 0 | 134 | | if (notifyEvent) |
| 0 | 135 | | OnValueChanged?.Invoke(currentPage); |
| 0 | 136 | | } |
| | 137 | |
|
| | 138 | | private void UpdateToggleStatus() |
| | 139 | | { |
| 0 | 140 | | for (int i = 0; i < buttons.Count; i++) |
| | 141 | | { |
| 0 | 142 | | var currentButton = buttons[i]; |
| 0 | 143 | | if (limitedPages) |
| 0 | 144 | | currentButton.gameObject.SetActive(ShouldShowButton(i)); |
| 0 | 145 | | currentButton.Toggle(i == currentPage); |
| | 146 | | } |
| 0 | 147 | | } |
| | 148 | |
|
| | 149 | | } |