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(); } } }