| | 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 | | { |
| 47 | 21 | | previousButton.onClick.AddListener(OnPreviousButtonDown); |
| 47 | 22 | | nextButton.onClick.AddListener(OnNextButtonDown); |
| | 23 | |
|
| 47 | 24 | | gameObject.SetActive(false); |
| | 25 | |
|
| 47 | 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, bool forceRebuild = false) |
| | 53 | | { |
| 6688 | 54 | | if (maxPages == this.maxPages && !forceRebuild) |
| | 55 | | { |
| 3886 | 56 | | return; |
| | 57 | | } |
| | 58 | |
|
| 2802 | 59 | | this.maxPages = maxPages; |
| | 60 | |
|
| 2802 | 61 | | currentPage = Mathf.Clamp(currentPage, 0, maxPages-1); |
| | 62 | |
|
| 2802 | 63 | | if (maxPages <= 1) |
| | 64 | | { |
| 2802 | 65 | | gameObject.SetActive(false); |
| 2802 | 66 | | OnValueChanged?.Invoke(0); |
| 225 | 67 | | return; |
| | 68 | | } |
| | 69 | |
|
| 0 | 70 | | gameObject.SetActive(true); |
| | 71 | |
|
| 0 | 72 | | EnsureButtons(); |
| 0 | 73 | | UpdateButtonsStatus(); |
| | 74 | |
|
| 0 | 75 | | LayoutRebuilder.ForceRebuildLayoutImmediate(pageButtonsParent); |
| 0 | 76 | | LayoutRebuilder.ForceRebuildLayoutImmediate(rectTransform); |
| 0 | 77 | | } |
| | 78 | | private void EnsureButtons() |
| | 79 | | { |
| 0 | 80 | | if (buttons.Count != maxPages) |
| | 81 | | { |
| 0 | 82 | | var diff = maxPages - buttons.Count; |
| | 83 | |
|
| 0 | 84 | | if (diff > 0) |
| | 85 | | { |
| 0 | 86 | | for (int i = 0; i < diff; i++) |
| | 87 | | { |
| 0 | 88 | | var instance = Instantiate(pageButtonPrefab, pageButtonsParent); |
| 0 | 89 | | buttons.Add(instance); |
| | 90 | | } |
| | 91 | | } |
| | 92 | | } |
| | 93 | |
|
| 0 | 94 | | for (int i = 0; i < buttons.Count; i++) |
| | 95 | | { |
| 0 | 96 | | UIPageButton uiPageButton = buttons[i]; |
| | 97 | |
|
| 0 | 98 | | if (i >= maxPages) |
| | 99 | | { |
| 0 | 100 | | uiPageButton.gameObject.SetActive(false); |
| | 101 | |
|
| 0 | 102 | | continue; |
| | 103 | | } |
| | 104 | |
|
| 0 | 105 | | uiPageButton.Initialize(i); |
| 0 | 106 | | uiPageButton.gameObject.SetActive(true); |
| 0 | 107 | | uiPageButton.OnPageClicked += OnPageClicked; |
| | 108 | | } |
| 0 | 109 | | } |
| | 110 | | private void UpdateButtonsStatus() |
| | 111 | | { |
| 0 | 112 | | UpdateToggleStatus(); |
| | 113 | |
|
| 0 | 114 | | OnValueChanged?.Invoke(currentPage); |
| 0 | 115 | | } |
| | 116 | | private void UpdateToggleStatus() |
| | 117 | | { |
| 0 | 118 | | for (int i = 0; i < buttons.Count; i++) |
| | 119 | | { |
| 0 | 120 | | var currentButton = buttons[i]; |
| 0 | 121 | | if (currentButton.isActiveAndEnabled) |
| 0 | 122 | | currentButton.Toggle(i == currentPage); |
| | 123 | | } |
| 0 | 124 | | } |
| | 125 | |
|
| | 126 | | } |