|
1 | | -using System.Collections; |
| 1 | +/// Credit Beka Westberg |
| 2 | +/// Sourced from - https://bitbucket.org/UnityUIExtensions/unity-ui-extensions/pull-requests/28 |
| 3 | +/// Updated by SimonDarksideJ - Added some exception management and a SetNewItems to replace the content programmatically |
| 4 | + |
| 5 | +using System.Collections; |
2 | 6 | using System.Collections.Generic; |
3 | 7 | using UnityEngine.EventSystems; |
4 | 8 | using UnityEngine.Events; |
@@ -185,11 +189,60 @@ private void Awake() |
185 | 189 | if (prevButton) |
186 | 190 | prevButton.GetComponent<Button>().onClick.AddListener(() => { PreviousItem(); }); |
187 | 191 |
|
188 | | - SetupDrivenTransforms(); |
189 | | - SetupSnapScroll(); |
190 | | - scrollRect.horizontalNormalizedPosition = 0; |
191 | | - _closestItem = 0; |
192 | | - GoTo(startInfo); |
| 192 | + if (IsScrollRectAvailable) |
| 193 | + { |
| 194 | + SetupDrivenTransforms(); |
| 195 | + SetupSnapScroll(); |
| 196 | + scrollRect.horizontalNormalizedPosition = 0; |
| 197 | + _closestItem = 0; |
| 198 | + GoTo(startInfo); |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + public void SetNewItems(ref List<Transform> newItems) |
| 203 | + { |
| 204 | + if (scrollRect && contentTransform) |
| 205 | + { |
| 206 | + for (int i = scrollRect.content.childCount - 1; i >= 0; i--) |
| 207 | + { |
| 208 | + Transform child = contentTransform.GetChild(i); |
| 209 | + child.SetParent(null); |
| 210 | + GameObject.DestroyImmediate(child.gameObject); |
| 211 | + } |
| 212 | + |
| 213 | + foreach (Transform item in newItems) |
| 214 | + { |
| 215 | + GameObject newItem = item.gameObject; |
| 216 | + if (newItem.IsPrefab()) |
| 217 | + { |
| 218 | + newItem = Instantiate(item.gameObject, contentTransform); |
| 219 | + } |
| 220 | + else |
| 221 | + { |
| 222 | + newItem.transform.SetParent(contentTransform); |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + SetupDrivenTransforms(); |
| 227 | + SetupSnapScroll(); |
| 228 | + scrollRect.horizontalNormalizedPosition = 0; |
| 229 | + _closestItem = 0; |
| 230 | + GoTo(startInfo); |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + private bool IsScrollRectAvailable |
| 235 | + { |
| 236 | + get |
| 237 | + { |
| 238 | + if (scrollRect && |
| 239 | + contentTransform && |
| 240 | + contentTransform.childCount > 0) |
| 241 | + { |
| 242 | + return true; |
| 243 | + } |
| 244 | + return false; |
| 245 | + } |
193 | 246 | } |
194 | 247 |
|
195 | 248 | private void OnDisable() |
@@ -470,16 +523,22 @@ public void OnBeginDrag(PointerEventData ped) |
470 | 523 |
|
471 | 524 | public void OnEndDrag(PointerEventData ped) |
472 | 525 | { |
473 | | - StartCoroutine("SlideAndLerp"); |
| 526 | + if (IsScrollRectAvailable) |
| 527 | + { |
| 528 | + StartCoroutine("SlideAndLerp"); |
| 529 | + } |
474 | 530 | } |
475 | 531 |
|
476 | 532 | private void Update() |
477 | 533 | { |
478 | | - if (_closestItem != ClosestItemIndex) |
| 534 | + if (IsScrollRectAvailable) |
479 | 535 | { |
480 | | - CurrentItemChanged.Invoke(ClosestItemIndex); |
481 | | - ChangePaginationInfo(ClosestItemIndex); |
482 | | - _closestItem = ClosestItemIndex; |
| 536 | + if (_closestItem != ClosestItemIndex) |
| 537 | + { |
| 538 | + CurrentItemChanged.Invoke(ClosestItemIndex); |
| 539 | + ChangePaginationInfo(ClosestItemIndex); |
| 540 | + _closestItem = ClosestItemIndex; |
| 541 | + } |
483 | 542 | } |
484 | 543 | } |
485 | 544 |
|
|
0 commit comments