using UnityEngine; using System.Collections; /// /// 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. /// 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(); } /// /// 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. /// 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 }