5using System.Collections.Generic;
21 [AddComponentMenu(
"UI/Extensions/UI Infinite Scroll")]
25 [Tooltip(
"If false, will Init automatically, otherwise you need to call Init() method")]
27 private ScrollRect _scrollRect;
28 private ContentSizeFitter _contentSizeFitter;
29 private VerticalLayoutGroup _verticalLayoutGroup;
30 private HorizontalLayoutGroup _horizontalLayoutGroup;
31 private GridLayoutGroup _gridLayoutGroup;
32 private bool _isVertical =
false;
33 private bool _isHorizontal =
false;
34 private float _disableMarginX = 0;
35 private float _disableMarginY = 0;
36 private bool _hasDisabledGridComponents =
false;
37 private List<RectTransform> items =
new List<RectTransform>();
38 private Vector2 _newAnchoredPosition = Vector2.zero;
40 private float _treshold = 100f;
41 private int _itemCount = 0;
42 private float _recordOffsetX = 0;
43 private float _recordOffsetY = 0;
53 if (GetComponent<ScrollRect>() !=
null)
55 _scrollRect = GetComponent<ScrollRect>();
56 _scrollRect.onValueChanged.AddListener(
OnScroll);
57 _scrollRect.movementType = ScrollRect.MovementType.Unrestricted;
59 for (
int i = 0; i < _scrollRect.content.childCount; i++)
61 items.Add(_scrollRect.content.GetChild(i).GetComponent<RectTransform>());
63 if (_scrollRect.content.GetComponent<VerticalLayoutGroup>() !=
null)
65 _verticalLayoutGroup = _scrollRect.content.GetComponent<VerticalLayoutGroup>();
67 if (_scrollRect.content.GetComponent<HorizontalLayoutGroup>() !=
null)
69 _horizontalLayoutGroup = _scrollRect.content.GetComponent<HorizontalLayoutGroup>();
71 if (_scrollRect.content.GetComponent<GridLayoutGroup>() !=
null)
73 _gridLayoutGroup = _scrollRect.content.GetComponent<GridLayoutGroup>();
75 if (_scrollRect.content.GetComponent<ContentSizeFitter>() !=
null)
77 _contentSizeFitter = _scrollRect.content.GetComponent<ContentSizeFitter>();
80 _isHorizontal = _scrollRect.horizontal;
81 _isVertical = _scrollRect.vertical;
83 if (_isHorizontal && _isVertical)
85 Debug.LogError(
"UI_InfiniteScroll doesn't support scrolling in both directions, please choose one direction (horizontal or vertical)");
88 _itemCount = _scrollRect.content.childCount;
92 Debug.LogError(
"UI_InfiniteScroll => No ScrollRect component found");
96 void DisableGridComponents()
100 _recordOffsetY = items[1].GetComponent<RectTransform>().anchoredPosition.y - items[0].GetComponent<RectTransform>().anchoredPosition.y;
101 if (_recordOffsetY < 0)
103 _recordOffsetY *= -1;
105 _disableMarginY = _recordOffsetY * _itemCount / 2;
109 _recordOffsetX = items[1].GetComponent<RectTransform>().anchoredPosition.x - items[0].GetComponent<RectTransform>().anchoredPosition.x;
110 if (_recordOffsetX < 0)
112 _recordOffsetX *= -1;
114 _disableMarginX = _recordOffsetX * _itemCount / 2;
117 if (_verticalLayoutGroup)
119 _verticalLayoutGroup.enabled =
false;
121 if (_horizontalLayoutGroup)
123 _horizontalLayoutGroup.enabled =
false;
125 if (_contentSizeFitter)
127 _contentSizeFitter.enabled =
false;
129 if (_gridLayoutGroup)
131 _gridLayoutGroup.enabled =
false;
133 _hasDisabledGridComponents =
true;
138 if (!_hasDisabledGridComponents)
139 DisableGridComponents();
141 for (
int i = 0; i < items.Count; i++)
145 if (_scrollRect.transform.InverseTransformPoint(items[i].gameObject.transform.position).x > _disableMarginX + _treshold)
147 _newAnchoredPosition = items[i].anchoredPosition;
148 _newAnchoredPosition.x -= _itemCount * _recordOffsetX;
149 items[i].anchoredPosition = _newAnchoredPosition;
150 _scrollRect.content.GetChild(_itemCount - 1).transform.SetAsFirstSibling();
152 else if (_scrollRect.transform.InverseTransformPoint(items[i].gameObject.transform.position).x < -_disableMarginX)
154 _newAnchoredPosition = items[i].anchoredPosition;
155 _newAnchoredPosition.x += _itemCount * _recordOffsetX;
156 items[i].anchoredPosition = _newAnchoredPosition;
157 _scrollRect.content.GetChild(0).transform.SetAsLastSibling();
163 if (_scrollRect.transform.InverseTransformPoint(items[i].gameObject.transform.position).y > _disableMarginY + _treshold)
165 _newAnchoredPosition = items[i].anchoredPosition;
166 _newAnchoredPosition.y -= _itemCount * _recordOffsetY;
167 items[i].anchoredPosition = _newAnchoredPosition;
168 _scrollRect.content.GetChild(_itemCount - 1).transform.SetAsFirstSibling();
170 else if (_scrollRect.transform.InverseTransformPoint(items[i].gameObject.transform.position).y < -_disableMarginY)
172 _newAnchoredPosition = items[i].anchoredPosition;
173 _newAnchoredPosition.y += _itemCount * _recordOffsetY;
174 items[i].anchoredPosition = _newAnchoredPosition;
175 _scrollRect.content.GetChild(0).transform.SetAsLastSibling();
Credit Erdener Gonenc - @PixelEnvision.