1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- using UnityEngine;
- using System.Collections;
- /// <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;
- public bool WinCondomPages;
- public UILabel LabelPages;
- int _currentPageIndex;
- 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
- }
|