PagingDialog.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. private UIElementTransition transaction;
  18. void Star()
  19. {
  20. //transaction = GetComponent<>()
  21. }
  22. void OnEnable()
  23. {
  24. _currentPageIndex = 0;
  25. LabelPages.text = string.Format("{0}/{1}", _currentPageIndex + 1, pages.Length);
  26. RefreshPage();
  27. }
  28. /// <summary>
  29. /// Enables the current page, disables the others.
  30. /// Updates the buttons state according to the current page.
  31. /// This needs to be called whenever the current page index is changed.
  32. /// </summary>
  33. void RefreshPage()
  34. {
  35. for (int i = 0; i < pages.Length; i++)
  36. {
  37. bool isPageActive = _currentPageIndex == i;
  38. if(pages[i].GetComponent<UIElementTransition>()!=null)
  39. {
  40. if (isPageActive)
  41. {
  42. pages[i].GetComponent<UIElementTransition>().Show();
  43. }
  44. else
  45. {
  46. pages[i].GetComponent<UIElementTransition>().Hide();
  47. }
  48. }
  49. else
  50. {
  51. pages[i].SetActive(isPageActive);
  52. }
  53. //
  54. }
  55. //prevButton enable/disable
  56. if (_currentPageIndex == 0)
  57. {
  58. UIUtils.EnableWidgets(prevButton, false);
  59. } else
  60. {
  61. UIUtils.EnableWidgets(prevButton, true);
  62. }
  63. //nextButton enable/disable
  64. if (_currentPageIndex == pages.Length - 1)
  65. {
  66. UIUtils.EnableWidgets(nextButton, false);
  67. } else
  68. {
  69. UIUtils.EnableWidgets(nextButton, true);
  70. }
  71. }
  72. #region Next/Prev button messages
  73. void OnNextPageButtonMsg()
  74. {
  75. if (_currentPageIndex < pages.Length - 1)
  76. {
  77. ++_currentPageIndex;
  78. RefreshPage();
  79. }
  80. LabelPages.text = string.Format("{0}/{1}", _currentPageIndex + 1, pages.Length);
  81. }
  82. void OnPrevPageButtonMsg()
  83. {
  84. if (_currentPageIndex > 0)
  85. {
  86. --_currentPageIndex;
  87. RefreshPage();
  88. }
  89. LabelPages.text = string.Format("{0}/{1}", _currentPageIndex + 1, pages.Length);
  90. }
  91. #endregion
  92. }