Skip to content
This repository was archived by the owner on Mar 20, 2023. It is now read-only.

Commit b268970

Browse files
Merged in ContentScrollSnapupdate (pull request #103)
ContentScrollSnapupdate
2 parents 48fc295 + 5a3621a commit b268970

File tree

2 files changed

+89
-12
lines changed

2 files changed

+89
-12
lines changed

Runtime/Scripts/Layout/ContentScrollSnapHorizontal.cs

Lines changed: 70 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
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;
26
using System.Collections.Generic;
37
using UnityEngine.EventSystems;
48
using UnityEngine.Events;
@@ -185,11 +189,60 @@ private void Awake()
185189
if (prevButton)
186190
prevButton.GetComponent<Button>().onClick.AddListener(() => { PreviousItem(); });
187191

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+
}
193246
}
194247

195248
private void OnDisable()
@@ -470,16 +523,22 @@ public void OnBeginDrag(PointerEventData ped)
470523

471524
public void OnEndDrag(PointerEventData ped)
472525
{
473-
StartCoroutine("SlideAndLerp");
526+
if (IsScrollRectAvailable)
527+
{
528+
StartCoroutine("SlideAndLerp");
529+
}
474530
}
475531

476532
private void Update()
477533
{
478-
if (_closestItem != ClosestItemIndex)
534+
if (IsScrollRectAvailable)
479535
{
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+
}
483542
}
484543
}
485544

Runtime/Scripts/Utilities/ExtentionMethods.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace UnityEngine.UI.Extensions
1+
using System;
2+
3+
namespace UnityEngine.UI.Extensions
24
{
35
public static class ExtentionMethods
46
{
@@ -11,5 +13,21 @@ public static T GetOrAddComponent<T>(this GameObject child) where T : Component
1113
}
1214
return result;
1315
}
16+
17+
public static bool IsPrefab(this GameObject gameObject)
18+
{
19+
if (gameObject == null)
20+
{
21+
throw new ArgumentNullException(nameof(gameObject));
22+
}
23+
24+
return
25+
!gameObject.scene.IsValid() &&
26+
!gameObject.scene.isLoaded &&
27+
gameObject.GetInstanceID() >= 0 &&
28+
// I noticed that ones with IDs under 0 were objects I didn't recognize
29+
!gameObject.hideFlags.HasFlag(HideFlags.HideInHierarchy);
30+
// I don't care about GameObjects *inside* prefabs, just the overall prefab.
31+
}
1432
}
1533
}

0 commit comments

Comments
 (0)