2
0

PaginationManager.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System.Collections.Generic;
  2. /// Credit Brogan King (@BroganKing)
  3. /// Original Sourced from - https://bitbucket.org/UnityUIExtensions/unity-ui-extensions/issues/158/pagination-script
  4. using System.Linq;
  5. namespace UnityEngine.UI.Extensions
  6. {
  7. [AddComponentMenu("UI/Extensions/Pagination Manager")]
  8. public class PaginationManager : ToggleGroup
  9. {
  10. private List<Toggle> m_PaginationChildren;
  11. [SerializeField]
  12. private ScrollSnapBase scrollSnap = null;
  13. private bool isAClick;
  14. public int CurrentPage
  15. {
  16. get { return scrollSnap.CurrentPage; }
  17. }
  18. protected PaginationManager()
  19. { }
  20. // Use this for initialization
  21. protected override void Start()
  22. {
  23. base.Start();
  24. if (scrollSnap == null)
  25. {
  26. Debug.LogError("A ScrollSnap script must be attached");
  27. return;
  28. }
  29. // do not want the scroll snap pagination
  30. if (scrollSnap.Pagination)
  31. scrollSnap.Pagination = null;
  32. // set scroll snap listeners
  33. scrollSnap.OnSelectionPageChangedEvent.AddListener(SetToggleGraphics);
  34. scrollSnap.OnSelectionChangeEndEvent.AddListener(OnPageChangeEnd);
  35. ResetPaginationChildren();
  36. }
  37. /// <summary>
  38. /// Remake the internal list of child toggles (m_PaginationChildren).
  39. /// Used after adding/removing a toggle.
  40. /// </summary>
  41. public void ResetPaginationChildren()
  42. {
  43. // add selectables to list
  44. m_PaginationChildren = GetComponentsInChildren<Toggle>().ToList<Toggle>();
  45. for (int i = 0; i < m_PaginationChildren.Count; i++)
  46. {
  47. m_PaginationChildren[i].onValueChanged.AddListener(ToggleClick);
  48. m_PaginationChildren[i].group = this;
  49. m_PaginationChildren[i].isOn = false;
  50. }
  51. // set toggles on start
  52. SetToggleGraphics(CurrentPage);
  53. // warn user that they have uneven amount of pagination toggles to page count
  54. if (m_PaginationChildren.Count != scrollSnap._scroll_rect.content.childCount)
  55. Debug.LogWarning("Uneven pagination icon to page count");
  56. }
  57. /// <summary>
  58. /// Calling from other scripts if you need to change screens programmatically
  59. /// </summary>
  60. /// <param name="pageNo"></param>
  61. public void GoToScreen(int pageNo)
  62. {
  63. scrollSnap.GoToScreen(pageNo, true);
  64. }
  65. /// <summary>
  66. /// Calls GoToScreen() based on the index of toggle that was pressed
  67. /// </summary>
  68. /// <param name="target"></param>
  69. private void ToggleClick(Toggle target)
  70. {
  71. if (!target.isOn)
  72. {
  73. isAClick = true;
  74. GoToScreen(m_PaginationChildren.IndexOf(target));
  75. }
  76. }
  77. private void ToggleClick(bool toggle)
  78. {
  79. if (toggle)
  80. {
  81. for (int i = 0; i < m_PaginationChildren.Count; i++)
  82. {
  83. if (m_PaginationChildren[i].isOn && !scrollSnap._suspendEvents)
  84. {
  85. GoToScreen(i);
  86. break;
  87. }
  88. }
  89. }
  90. }
  91. /// <summary>
  92. /// Calls GoToScreen() based on the index of toggle that was pressed
  93. /// </summary>
  94. /// <param name="target"></param>
  95. private void ToggleClick(int target)
  96. {
  97. isAClick = true;
  98. GoToScreen(target);
  99. }
  100. private void SetToggleGraphics(int pageNo)
  101. {
  102. if (!isAClick)
  103. {
  104. m_PaginationChildren[pageNo].isOn = true;
  105. //for (int i = 0; i < m_PaginationChildren.Count; i++)
  106. //{
  107. // m_PaginationChildren[i].isOn = pageNo == i ? true : false;
  108. //}
  109. }
  110. }
  111. private void OnPageChangeEnd(int pageNo)
  112. {
  113. isAClick = false;
  114. }
  115. }
  116. }