PagingDialog.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using UnityEngine;
  2. using System.Collections;
  3. /// <summary>
  4. /// This component is responsible for having paging with a next and prev buttons.
  5. /// It enables/disables the buttons according to whether there are more pages to go through
  6. /// Each page is a GameObject, which is activated when the user is paging.
  7. /// The next/prev buttons need to send a message to this component accordingly (OnNextPageButtonMsg and OnPrevPageButtonMsg).
  8. /// For sending such a message the buttons can easily use UIButtonMessage component.
  9. /// </summary>
  10. public class PagingDialog : MonoBehaviour
  11. {
  12. public GameObject[] pages;
  13. public GameObject prevButton, nextButton;
  14. public bool WinCondomPages;
  15. public UILabel LabelPages;
  16. int _currentPageIndex;
  17. void OnEnable()
  18. {
  19. _currentPageIndex = 0;
  20. if (WinCondomPages)
  21. {
  22. LabelPages.text = string.Format("{0}/{1}", _currentPageIndex + 1, pages.Length);
  23. }
  24. RefreshPage();
  25. }
  26. /// <summary>
  27. /// Enables the current page, disables the others.
  28. /// Updates the buttons state according to the current page.
  29. /// This needs to be called whenever the current page index is changed.
  30. /// </summary>
  31. void RefreshPage()
  32. {
  33. for (int i = 0; i < pages.Length; i++)
  34. {
  35. bool isPageActive = _currentPageIndex == i;
  36. pages[i].SetActive(isPageActive);
  37. }
  38. //prevButton enable/disable
  39. if (_currentPageIndex == 0)
  40. {
  41. UIUtils.EnableWidgets(prevButton, false);
  42. } else
  43. {
  44. UIUtils.EnableWidgets(prevButton, true);
  45. }
  46. //nextButton enable/disable
  47. if (_currentPageIndex == pages.Length - 1)
  48. {
  49. UIUtils.EnableWidgets(nextButton, false);
  50. } else
  51. {
  52. UIUtils.EnableWidgets(nextButton, true);
  53. }
  54. }
  55. #region Next/Prev button messages
  56. void OnNextPageButtonMsg()
  57. {
  58. if (_currentPageIndex < pages.Length - 1)
  59. {
  60. ++_currentPageIndex;
  61. RefreshPage();
  62. }
  63. if (WinCondomPages)
  64. {
  65. LabelPages.text = string.Format("{0}/{1}", _currentPageIndex + 1, pages.Length);
  66. }
  67. }
  68. void OnPrevPageButtonMsg()
  69. {
  70. if (_currentPageIndex > 0)
  71. {
  72. --_currentPageIndex;
  73. RefreshPage();
  74. }
  75. if (WinCondomPages)
  76. {
  77. LabelPages.text = string.Format("{0}/{1}", _currentPageIndex + 1, pages.Length);
  78. }
  79. }
  80. #endregion
  81. }