Tanoda
PaginationManager.cs
Go to the documentation of this file.
1using System.Collections.Generic;
4
5using System.Linq;
6
8{
9 [AddComponentMenu("UI/Extensions/Pagination Manager")]
10 public class PaginationManager : ToggleGroup
11 {
12 private List<Toggle> m_PaginationChildren;
13
14 [SerializeField]
15 private ScrollSnapBase scrollSnap = null;
16
17 private bool isAClick;
18
19 public int CurrentPage
20 {
21 get { return scrollSnap.CurrentPage; }
22 }
23
25 { }
26
27
28 // Use this for initialization
29 protected override void Start()
30 {
31 base.Start();
32
33 if (scrollSnap == null)
34 {
35 Debug.LogError("A ScrollSnap script must be attached");
36 return;
37 }
38
39 // do not want the scroll snap pagination
40 if (scrollSnap.Pagination)
41 scrollSnap.Pagination = null;
42
43 // set scroll snap listeners
44 scrollSnap.OnSelectionPageChangedEvent.AddListener(SetToggleGraphics);
45 scrollSnap.OnSelectionChangeEndEvent.AddListener(OnPageChangeEnd);
46
47 // add selectables to list
48 m_PaginationChildren = GetComponentsInChildren<Toggle>().ToList<Toggle>();
49 for (int i = 0; i < m_PaginationChildren.Count; i++)
50 {
51 m_PaginationChildren[i].onValueChanged.AddListener(ToggleClick);
52 m_PaginationChildren[i].group = this;
53 m_PaginationChildren[i].isOn = false;
54 }
55
56 // set toggles on start
57 SetToggleGraphics(CurrentPage);
58
59 // warn user that they have uneven amount of pagination toggles to page count
60 if (m_PaginationChildren.Count != scrollSnap._scroll_rect.content.childCount)
61 Debug.LogWarning("Uneven pagination icon to page count");
62 }
63
64
69 public void GoToScreen(int pageNo)
70 {
71 scrollSnap.GoToScreen(pageNo);
72 }
73
74
79 private void ToggleClick(Toggle target)
80 {
81 if (!target.isOn)
82 {
83 isAClick = true;
84 GoToScreen(m_PaginationChildren.IndexOf(target));
85 }
86
87 }
88
89 private void ToggleClick(bool toggle)
90 {
91 if (toggle)
92 {
93 for (int i = 0; i < m_PaginationChildren.Count; i++)
94 {
95 if (m_PaginationChildren[i].isOn)
96 {
97 GoToScreen(i);
98 break;
99 }
100 }
101 }
102 }
103
108 private void ToggleClick(int target)
109 {
110 isAClick = true;
111 GoToScreen(target);
112 }
113
114 private void SetToggleGraphics(int pageNo)
115 {
116 if (!isAClick)
117 {
118 m_PaginationChildren[pageNo].isOn = true;
119 //for (int i = 0; i < m_PaginationChildren.Count; i++)
120 //{
121 // m_PaginationChildren[i].isOn = pageNo == i ? true : false;
122 //}
123 }
124 }
125
126 private void OnPageChangeEnd(int pageNo)
127 {
128 isAClick = false;
129 }
130 }
131}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
void GoToScreen(int pageNo)
Calling from other scripts if you need to change screens programmatically
SelectionPageChangedEvent OnSelectionPageChangedEvent
SelectionChangeEndEvent OnSelectionChangeEndEvent
void GoToScreen(int screenIndex)
Function for switching to a specific screen *Note, this is based on a 0 starting index - 0 to x
Credit Erdener Gonenc - @PixelEnvision.