Updated Infinite scroll to work with content of different sizes

pull/413/head
Simon Jackson 2021-09-05 15:23:48 +01:00
parent b0f161a4e0
commit 0410ca53d7
1 changed files with 24 additions and 13 deletions

View File

@ -38,7 +38,7 @@ namespace UnityEngine.UI.Extensions
protected List<RectTransform> items = new List<RectTransform>();
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<RectTransform>().sizeDelta * 0.5f;
if (_isHorizontal && _isVertical)
{
@ -175,41 +176,51 @@ namespace UnityEngine.UI.Extensions
if (!_hasDisabledGridComponents)
DisableGridComponents();
var firstChild = _scrollRect.content.GetChild(0).GetComponent<RectTransform>();
var lastChild = _scrollRect.content.GetChild(_itemCount - 1).GetComponent<RectTransform>();
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();
}
}
}