12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- using UnityEngine;
- using System.Collections;
- using TMPro;
- using UnityEngine.UI;
- /// <summary>
- /// This component is responsible for having paging with a next and prev buttons.
- /// It enables/disables the buttons according to whether there are more pages to go through
- /// Each page is a GameObject, which is activated when the user is paging.
- /// The next/prev buttons need to send a message to this component accordingly (OnNextPageButtonMsg and OnPrevPageButtonMsg).
- /// For sending such a message the buttons can easily use UIButtonMessage component.
- /// </summary>
- public class PagingDialog : MonoBehaviour
- {
- public GameObject[] pages;
- public GameObject prevButton, nextButton;
- private Button prevButtonUI, nextButtonUI;
- public bool WinCondomPages;
- public TextMeshProUGUI LabelPages;
- int _currentPageIndex;
- void Start()
- {
- prevButtonUI = prevButton.GetComponent<Button>();
- prevButtonUI.onClick.AddListener(OnPrevPageButtonMsg);
- nextButtonUI = nextButton.GetComponent<Button>();
- nextButtonUI.onClick.AddListener(OnNextPageButtonMsg);
- }
- void OnEnable()
- {
- _currentPageIndex = 0;
- if (WinCondomPages)
- {
- LabelPages.text = string.Format("{0}/{1}", _currentPageIndex + 1, pages.Length);
- }
- RefreshPage();
- }
- /// <summary>
- /// Enables the current page, disables the others.
- /// Updates the buttons state according to the current page.
- /// This needs to be called whenever the current page index is changed.
- /// </summary>
- void RefreshPage()
- {
- for (int i = 0; i < pages.Length; i++)
- {
- bool isPageActive = _currentPageIndex == i;
- pages[i].SetActive(isPageActive);
- }
- //prevButton enable/disable
- if (_currentPageIndex == 0)
- {
- //UIUtils.EnableWidgets(prevButton, false);
- } else
- {
- //UIUtils.EnableWidgets(prevButton, true);
- }
- //nextButton enable/disable
- if (_currentPageIndex == pages.Length - 1)
- {
- //UIUtils.EnableWidgets(nextButton, false);
- } else
- {
- //UIUtils.EnableWidgets(nextButton, true);
- }
- }
- #region Next/Prev button messages
- void OnNextPageButtonMsg()
- {
- if (_currentPageIndex < pages.Length - 1)
- {
- ++_currentPageIndex;
- RefreshPage();
- }
- if (WinCondomPages)
- {
- LabelPages.text = string.Format("{0}/{1}", _currentPageIndex + 1, pages.Length);
- }
- }
- void OnPrevPageButtonMsg()
- {
- if (_currentPageIndex > 0)
- {
- --_currentPageIndex;
- RefreshPage();
- }
- if (WinCondomPages)
- {
- LabelPages.text = string.Format("{0}/{1}", _currentPageIndex + 1, pages.Length);
- }
- }
- #endregion
- }
|