From 0410ca53d7f740145119a80eef2065d38d8636e2 Mon Sep 17 00:00:00 2001 From: Simon Jackson Date: Sun, 5 Sep 2021 15:23:48 +0100 Subject: [PATCH 1/2] Updated Infinite scroll to work with content of different sizes --- .../Scripts/Utilities/UI_InfiniteScroll.cs | 37 ++++++++++++------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/Runtime/Scripts/Utilities/UI_InfiniteScroll.cs b/Runtime/Scripts/Utilities/UI_InfiniteScroll.cs index 742788b..c4100ff 100644 --- a/Runtime/Scripts/Utilities/UI_InfiniteScroll.cs +++ b/Runtime/Scripts/Utilities/UI_InfiniteScroll.cs @@ -38,7 +38,7 @@ namespace UnityEngine.UI.Extensions protected List items = new List(); private Vector2 _newAnchoredPosition = Vector2.zero; //TO DISABLE FLICKERING OBJECT WHEN SCROLL VIEW IS IDLE IN BETWEEN OBJECTS - private float _threshold = 100f; + private Vector2 _threshold = Vector2.zero; private int _itemCount = 0; private float _recordOffsetX = 0; private float _recordOffsetY = 0; @@ -116,6 +116,7 @@ namespace UnityEngine.UI.Extensions _isHorizontal = _scrollRect.horizontal; _isVertical = _scrollRect.vertical; + _threshold = _scrollRect.GetComponent().sizeDelta * 0.5f; if (_isHorizontal && _isVertical) { @@ -175,41 +176,51 @@ namespace UnityEngine.UI.Extensions if (!_hasDisabledGridComponents) DisableGridComponents(); + var firstChild = _scrollRect.content.GetChild(0).GetComponent(); + var lastChild = _scrollRect.content.GetChild(_itemCount - 1).GetComponent(); + for (int i = 0; i < items.Count; i++) { if (_isHorizontal) { - if (_scrollRect.transform.InverseTransformPoint(items[i].gameObject.transform.position).x > _disableMarginX + _threshold) + if (_scrollRect.transform.InverseTransformPoint(items[i].gameObject.transform.position).x > items[i].sizeDelta.x + _threshold.x && items[i] == lastChild) { + //Moving before first child ( slide right) _newAnchoredPosition = items[i].anchoredPosition; - _newAnchoredPosition.x -= _itemCount * _recordOffsetX; + _newAnchoredPosition.x = firstChild.anchoredPosition.x - items[i].sizeDelta.x; items[i].anchoredPosition = _newAnchoredPosition; - _scrollRect.content.GetChild(_itemCount - 1).transform.SetAsFirstSibling(); + lastChild.transform.SetAsFirstSibling(); } - else if (_scrollRect.transform.InverseTransformPoint(items[i].gameObject.transform.position).x < -_disableMarginX) + else if (_scrollRect.transform.InverseTransformPoint(items[i].gameObject.transform.position).x < -items[i].sizeDelta.x - _threshold.x - 100 && items[i] == firstChild) { + //Moving before first child (slide left) _newAnchoredPosition = items[i].anchoredPosition; - _newAnchoredPosition.x += _itemCount * _recordOffsetX; + _newAnchoredPosition.x = lastChild.anchoredPosition.x + lastChild.sizeDelta.x; items[i].anchoredPosition = _newAnchoredPosition; - _scrollRect.content.GetChild(0).transform.SetAsLastSibling(); + firstChild.transform.SetAsLastSibling(); + } } if (_isVertical) { - if (_scrollRect.transform.InverseTransformPoint(items[i].gameObject.transform.position).y > _disableMarginY + _threshold) + if (_scrollRect.transform.InverseTransformPoint(items[i].gameObject.transform.position).y > items[i].sizeDelta.y + _threshold.y && items[i] == firstChild) { + //Moving after last child ( slide up) _newAnchoredPosition = items[i].anchoredPosition; - _newAnchoredPosition.y -= _itemCount * _recordOffsetY; + _newAnchoredPosition.y = lastChild.anchoredPosition.y - items[i].sizeDelta.y; + items[i].anchoredPosition = _newAnchoredPosition; - _scrollRect.content.GetChild(_itemCount - 1).transform.SetAsFirstSibling(); + firstChild.transform.SetAsLastSibling(); } - else if (_scrollRect.transform.InverseTransformPoint(items[i].gameObject.transform.position).y < -_disableMarginY) + else if (_scrollRect.transform.InverseTransformPoint(items[i].gameObject.transform.position).y < -items[i].sizeDelta.y - _threshold.y - 100 && items[i] == lastChild) { + //Moving before first child (slidw down) _newAnchoredPosition = items[i].anchoredPosition; - _newAnchoredPosition.y += _itemCount * _recordOffsetY; + _newAnchoredPosition.y = firstChild.anchoredPosition.y + firstChild.sizeDelta.y; + items[i].anchoredPosition = _newAnchoredPosition; - _scrollRect.content.GetChild(0).transform.SetAsLastSibling(); + lastChild.transform.SetAsFirstSibling(); } } } From 1c68432e02ec6b9e3c081783dcc5017d542eee4e Mon Sep 17 00:00:00 2001 From: Simon Jackson Date: Sun, 5 Sep 2021 15:33:24 +0100 Subject: [PATCH 2/2] Clean-up and reset pivots on scene start --- .../Scripts/Utilities/UI_InfiniteScroll.cs | 29 ++++--------------- 1 file changed, 6 insertions(+), 23 deletions(-) diff --git a/Runtime/Scripts/Utilities/UI_InfiniteScroll.cs b/Runtime/Scripts/Utilities/UI_InfiniteScroll.cs index c4100ff..dbcdd3a 100644 --- a/Runtime/Scripts/Utilities/UI_InfiniteScroll.cs +++ b/Runtime/Scripts/Utilities/UI_InfiniteScroll.cs @@ -32,16 +32,12 @@ namespace UnityEngine.UI.Extensions private GridLayoutGroup _gridLayoutGroup; protected bool _isVertical = false; protected bool _isHorizontal = false; - private float _disableMarginX = 0; - private float _disableMarginY = 0; private bool _hasDisabledGridComponents = false; protected List items = new List(); private Vector2 _newAnchoredPosition = Vector2.zero; //TO DISABLE FLICKERING OBJECT WHEN SCROLL VIEW IS IDLE IN BETWEEN OBJECTS private Vector2 _threshold = Vector2.zero; private int _itemCount = 0; - private float _recordOffsetX = 0; - private float _recordOffsetY = 0; protected virtual void Awake() { @@ -81,6 +77,12 @@ namespace UnityEngine.UI.Extensions private void SetItems() { + //Remove Pivots from content as they mess up translation + foreach (RectTransform transform in _scrollRect.content.transform) + { + transform.pivot = Vector3.zero; + } + for (int i = 0; i < _scrollRect.content.childCount; i++) { items.Add(_scrollRect.content.GetChild(i).GetComponent()); @@ -133,25 +135,6 @@ namespace UnityEngine.UI.Extensions void DisableGridComponents() { - if (_isVertical) - { - _recordOffsetY = items[1].GetComponent().anchoredPosition.y - items[0].GetComponent().anchoredPosition.y; - if (_recordOffsetY < 0) - { - _recordOffsetY *= -1; - } - _disableMarginY = _recordOffsetY * _itemCount / 2; - } - if (_isHorizontal) - { - _recordOffsetX = items[1].GetComponent().anchoredPosition.x - items[0].GetComponent().anchoredPosition.x; - if (_recordOffsetX < 0) - { - _recordOffsetX *= -1; - } - _disableMarginX = _recordOffsetX * _itemCount / 2; - } - if (_verticalLayoutGroup) { _verticalLayoutGroup.enabled = false;