ScrollRect
How do I use Scroll Rect
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: ScrollSnap.cs
Copy
107 void Awake()
108 {
109 lerp = false;
110
111 scrollRect = gameObject.GetComponent
112 scrollRectTransform = gameObject.GetComponent
113 listContainerTransform = scrollRect.content;
114 listContainerRectTransform = listContainerTransform.GetComponent
115
116 rectTransform = listContainerTransform.gameObject.GetComponent
117 UpdateListItemsSize();
118 UpdateListItemPositions();
119
120 PageChanged(CurrentPage());
121
122 if (nextButton)
123 {
124 nextButton.GetComponent
125 {
126 NextScreen();
127 });
128 }
129
130 if (prevButton)
131 {
132 prevButton.GetComponent
133 {
134 PreviousScreen();
135 });
136 }
137 }
File name: ScrollSnap.cs
Copy
144 public void UpdateListItemsSize()
145 {
146 float size = 0;
147 float currentSize = 0;
148 if (direction == ScrollSnap.ScrollDirection.Horizontal)
149 {
150 size = scrollRectTransform.rect.width / itemsVisibleAtOnce;
151 currentSize = listContainerRectTransform.rect.width / itemsCount;
152 }
153 else
154 {
155 size = scrollRectTransform.rect.height / itemsVisibleAtOnce;
156 currentSize = listContainerRectTransform.rect.height / itemsCount;
157 }
158
159 itemSize = size;
160
161 if (linkScrolrectScrollSensitivity)
162 {
163 scrollRect.scrollSensitivity = itemSize;
164 }
165
166 if (autoLayoutItems && currentSize != size && itemsCount > 0)
167 {
168 if (direction == ScrollSnap.ScrollDirection.Horizontal)
169 {
170 foreach (var tr in listContainerTransform)
171 {
172 GameObject child = ((Transform)tr).gameObject;
173 if (child.activeInHierarchy)
174 {
175 var childLayout = child.GetComponent
176
177 if (childLayout == null)
178 {
179 childLayout = child.AddComponent
180 }
181
182 childLayout.minWidth = itemSize;
183 }
184 }
185 }
186 else
187 {
188 foreach (var tr in listContainerTransform)
189 {
190 GameObject child = ((Transform)tr).gameObject;
191 if (child.activeInHierarchy)
192 {
193 var childLayout = child.GetComponent
194
195 if (childLayout == null)
196 {
197 childLayout = child.AddComponent
198 }
199
200 childLayout.minHeight = itemSize;
201 }
202 }
203 }
204 }
205 }
File name: ScrollSnap.cs
Copy
207 public void UpdateListItemPositions()
208 {
209 if (!listContainerRectTransform.rect.size.Equals(listContainerCachedSize))
210 {
211 // checking how many children of list are active
212 int activeCount = 0;
213
214 foreach (var tr in listContainerTransform)
215 {
216 if (((Transform)tr).gameObject.activeInHierarchy)
217 {
218 activeCount++;
219 }
220 }
221
222 // if anything changed since last check reinitialize anchors list
223 itemsCount = 0;
224 Array.Resize(ref pageAnchorPositions, activeCount);
225
226 if (activeCount > 0)
227 {
228 pages = Mathf.Max(activeCount - itemsVisibleAtOnce + 1, 1);
229
230 if (direction == ScrollDirection.Horizontal)
231 {
232 // looking for list spanning range min/max
233 scrollRect.horizontalNormalizedPosition = 0;
234 listContainerMaxPosition = listContainerTransform.localPosition.x;
235 scrollRect.horizontalNormalizedPosition = 1;
236 listContainerMinPosition = listContainerTransform.localPosition.x;
237
238 listContainerSize = listContainerMaxPosition - listContainerMinPosition;
239
240 for (var i = 0; i < pages; i++)
241 {
242 pageAnchorPositions[i] = new Vector3(
243 listContainerMaxPosition - itemSize * i,
244 listContainerTransform.localPosition.y,
245 listContainerTransform.localPosition.z
246 );
247 }
248 }
249 else
250 {
251 //Debug.Log ("-------------looking for list spanning range----------------");
252 // looking for list spanning range
253 scrollRect.verticalNormalizedPosition = 1;
254 listContainerMinPosition = listContainerTransform.localPosition.y;
255 scrollRect.verticalNormalizedPosition = 0;
256 listContainerMaxPosition = listContainerTransform.localPosition.y;
257
258 listContainerSize = listContainerMaxPosition - listContainerMinPosition;
259
260 for (var i = 0; i < pages; i++)
261 {
262 pageAnchorPositions[i] = new Vector3(
263 listContainerTransform.localPosition.x,
264 listContainerMinPosition + itemSize * i,
265 listContainerTransform.localPosition.z
266 );
267 }
268 }
269
270 UpdateScrollbar(linkScrolbarSteps);
271 startingPage = Mathf.Min(startingPage, pages);
272 ResetPage();
273 }
274
275 if (itemsCount != activeCount)
276 {
277 PageChanged(CurrentPage());
278 }
279
280 itemsCount = activeCount;
281 listContainerCachedSize.Set(listContainerRectTransform.rect.size.x, listContainerRectTransform.rect.size.y);
282 }
283
284 }
File name: ScrollSnap.cs
Copy
286 public void ResetPage()
287 {
288 if (direction == ScrollDirection.Horizontal)
289 {
290 scrollRect.horizontalNormalizedPosition = pages > 1 ? (float)startingPage / (float)(pages - 1) : 0;
291 }
292 else
293 {
294 scrollRect.verticalNormalizedPosition = pages > 1 ? (float)(pages - startingPage - 1) / (float)(pages - 1) : 0;
295 }
296 }
File name: ScrollSnap.cs
Copy
298 protected void UpdateScrollbar(bool linkSteps)
299 {
300 if (linkSteps)
301 {
302 if (direction == ScrollDirection.Horizontal)
303 {
304 if (scrollRect.horizontalScrollbar != null)
305 {
306 scrollRect.horizontalScrollbar.numberOfSteps = pages;
307 }
308 }
309 else
310 {
311 if (scrollRect.verticalScrollbar != null)
312 {
313 scrollRect.verticalScrollbar.numberOfSteps = pages;
314 }
315 }
316 }
317 else
318 {
319 if (direction == ScrollDirection.Horizontal)
320 {
321 if (scrollRect.horizontalScrollbar != null)
322 {
323 scrollRect.horizontalScrollbar.numberOfSteps = 0;
324 }
325 }
326 else
327 {
328 if (scrollRect.verticalScrollbar != null)
329 {
330 scrollRect.verticalScrollbar.numberOfSteps = 0;
331 }
332 }
333 }
334 }
File name: ScrollSnapBase.cs
Copy
115 void Awake()
116 {
117 _scroll_rect = gameObject.GetComponent
118
119 if (_scroll_rect.horizontalScrollbar || _scroll_rect.verticalScrollbar)
120 {
121 Debug.LogWarning("Warning, using scrollbars with the Scroll Snap controls is not advised as it causes unpredictable results");
122 }
123
124 if (StartingScreen < 0)
125 {
126 StartingScreen = 0;
127 }
128
129 _screensContainer = _scroll_rect.content;
130 if (ChildObjects != null && ChildObjects.Length > 0)
131 {
132 if (_screensContainer.transform.childCount > 0)
133 {
134 Debug.LogError("ScrollRect Content has children, this is not supported when using managed Child Objects\n Either remove the ScrollRect Content children or clear the ChildObjects array");
135 return;
136 }
137
138 InitialiseChildObjectsFromArray();
139 }
140 else
141 {
142 InitialiseChildObjectsFromScene();
143 }
144
145 if (NextButton)
146 NextButton.GetComponent
147
148 if (PrevButton)
149 PrevButton.GetComponent
150 }
File name: ScrollSnapBase.cs
Copy
373 private void OnValidate()
374 {
375 var children = gameObject.GetComponent
376 if (children != 0 || ChildObjects != null)
377 {
378 var childCount = ChildObjects == null || ChildObjects.Length == 0 ? children : ChildObjects.Length;
379 if (StartingScreen > childCount - 1)
380 {
381 StartingScreen = childCount - 1;
382 }
383
384 if (StartingScreen < 0)
385 {
386 StartingScreen = 0;
387 }
388 }
389
390 if (MaskBuffer <= 0)
391 {
392 MaskBuffer = 1;
393 }
394
395 if (PageStep < 0)
396 {
397 PageStep = 0;
398 }
399
400 if (PageStep > 8)
401 {
402 PageStep = 9;
403 }
404 }
File name: UIVerticalScroller.cs
Copy
48 public void Awake()
49 {
50 var scrollRect = GetComponent
51 if (!_scrollingPanel)
52 {
53 _scrollingPanel = scrollRect.content;
54 }
55 if (!_center)
56 {
57 Debug.LogError("Please define the RectTransform for the Center viewport of the scrollable area");
58 }
59 if (_arrayOfElements == null || _arrayOfElements.Length == 0)
60 {
61 var childCount = scrollRect.content.childCount;
62 if (childCount > 0)
63 {
64 _arrayOfElements = new GameObject[childCount];
65 for (int i = 0; i < childCount; i++)
66 {
67 _arrayOfElements[i] = scrollRect.content.GetChild(i).gameObject;
68 }
69 }
70 }
71 }
File name: ScrollSnap.cs
Copy
33 protected override void Awake() {
34 base.Awake();
35 actualIndex = startingIndex;
36 cellIndex = startingIndex;
37 this.onLerpComplete = new OnLerpCompleteEvent();
38 this.onRelease = new OnReleaseEvent();
39 this.scrollRect = GetComponent
40 this.canvasGroup = GetComponent
41 this.content = scrollRect.content;
42 this.cellSize = content.GetComponent
43 content.anchoredPosition = new Vector2(-cellSize.x * cellIndex, content.anchoredPosition.y);
44 int count = LayoutElementCount();
45 SetContentSize(count);
46
47 if(startingIndex < count) {
48 MoveToIndex(startingIndex);
49 }
50 }
File name: ScrollSnap.cs
Copy
172 int CalculateMaxIndex() {
173 int cellPerFrame = Mathf.FloorToInt(scrollRect.GetComponent
174 return LayoutElementCount() - cellPerFrame;
175 }
ScrollRect 211 lượt xem
Gõ tìm kiếm nhanh...