PagingDialog.cs 2.4 KB

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