| | 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; |
| 900 | 15 | | private List<UIPageButton> buttons = new List<UIPageButton>(); |
| | 16 | | private int maxPages; |
| | 17 | | private int currentPage; |
| | 18 | |
|
| | 19 | | private void Awake() |
| | 20 | | { |
| 0 | 21 | | previousButton.onClick.AddListener(OnPreviousButtonDown); |
| 0 | 22 | | nextButton.onClick.AddListener(OnNextButtonDown); |
| | 23 | |
|
| 0 | 24 | | gameObject.SetActive(false); |
| | 25 | |
|
| 0 | 26 | | } |
| | 27 | | private void OnNextButtonDown() |
| | 28 | | { |
| 0 | 29 | | currentPage = (currentPage + 1 ) % maxPages; |
| 0 | 30 | | UpdateButtonsStatus(); |
| 0 | 31 | | } |
| | 32 | | private void OnPreviousButtonDown() |
| | 33 | | { |
| 0 | 34 | | if (currentPage - 1 < 0) |
| | 35 | | { |
| 0 | 36 | | currentPage = maxPages - 1; |
| 0 | 37 | | } |
| | 38 | | else |
| | 39 | | { |
| 0 | 40 | | currentPage = (currentPage - 1 ) % maxPages; |
| | 41 | | } |
| | 42 | |
|
| 0 | 43 | | UpdateButtonsStatus(); |
| 0 | 44 | | } |
| | 45 | |
|
| | 46 | | private void OnPageClicked(int pageNumber) |
| | 47 | | { |
| 0 | 48 | | currentPage = pageNumber; |
| 0 | 49 | | UpdateButtonsStatus(); |
| 0 | 50 | | } |
| | 51 | |
|
| | 52 | | public void Setup(int maxPages) |
| | 53 | | { |
| 6688 | 54 | | currentPage = 0; |
| 6688 | 55 | | this.maxPages = maxPages; |
| | 56 | |
|
| 6688 | 57 | | if (maxPages <= 1) |
| | 58 | | { |
| 6688 | 59 | | gameObject.SetActive(false); |
| 6688 | 60 | | OnValueChanged?.Invoke(0); |
| 496 | 61 | | return; |
| | 62 | | } |
| | 63 | |
|
| 0 | 64 | | gameObject.SetActive(true); |
| | 65 | |
|
| 0 | 66 | | EnsureButtons(); |
| 0 | 67 | | UpdateButtonsStatus(); |
| | 68 | |
|
| 0 | 69 | | LayoutRebuilder.ForceRebuildLayoutImmediate(pageButtonsParent); |
| 0 | 70 | | LayoutRebuilder.ForceRebuildLayoutImmediate(rectTransform); |
| 0 | 71 | | } |
| | 72 | | private void EnsureButtons() |
| | 73 | | { |
| 0 | 74 | | if (buttons.Count != maxPages) |
| | 75 | | { |
| 0 | 76 | | var diff = maxPages - buttons.Count; |
| | 77 | |
|
| 0 | 78 | | if (diff > 0) |
| | 79 | | { |
| 0 | 80 | | for (int i = 0; i < diff; i++) |
| | 81 | | { |
| 0 | 82 | | var instance = Instantiate(pageButtonPrefab, pageButtonsParent); |
| 0 | 83 | | buttons.Add(instance); |
| | 84 | | } |
| | 85 | | } |
| | 86 | | } |
| | 87 | |
|
| 0 | 88 | | for (int i = 0; i < buttons.Count; i++) |
| | 89 | | { |
| 0 | 90 | | UIPageButton uiPageButton = buttons[i]; |
| | 91 | |
|
| 0 | 92 | | if (i >= maxPages) |
| | 93 | | { |
| 0 | 94 | | uiPageButton.gameObject.SetActive(false); |
| | 95 | |
|
| 0 | 96 | | continue; |
| | 97 | | } |
| | 98 | |
|
| 0 | 99 | | uiPageButton.Initialize(i); |
| 0 | 100 | | uiPageButton.gameObject.SetActive(true); |
| 0 | 101 | | uiPageButton.OnPageClicked += OnPageClicked; |
| | 102 | | } |
| 0 | 103 | | } |
| | 104 | | private void UpdateButtonsStatus() |
| | 105 | | { |
| 0 | 106 | | UpdateToggleStatus(); |
| | 107 | |
|
| 0 | 108 | | OnValueChanged?.Invoke(currentPage); |
| 0 | 109 | | } |
| | 110 | | private void UpdateToggleStatus() |
| | 111 | | { |
| 0 | 112 | | for (int i = 0; i < buttons.Count; i++) |
| | 113 | | { |
| 0 | 114 | | var currentButton = buttons[i]; |
| 0 | 115 | | currentButton.Toggle(i == currentPage); |
| | 116 | | } |
| 0 | 117 | | } |
| | 118 | |
|
| | 119 | | } |