-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathReuseScrollView.cs
325 lines (282 loc) · 9.82 KB
/
ReuseScrollView.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;
using UnityEngine.UI;
public enum Direction
{
Vertical,
Horizontal
}
[RequireComponent(typeof(RectTransform), typeof(ScrollRect))]
public class ReuseScrollView : MonoBehaviour {
public Direction ScrollDirection;
private GameObject itemObject;
private RectTransform rectTransform;
private ScrollRect scrollRect;
private Vector2 scrollPosition;
private readonly LinkedList<ReuseItem> itemsLinkedList = new LinkedList<ReuseItem>();
private List<ReuseItemData> itemDataList = new List<ReuseItemData>();
public static float Spacing;
void Awake()
{
rectTransform = GetComponent<RectTransform>();
scrollRect = GetComponent<ScrollRect>();
scrollRect.horizontal = ScrollDirection == Direction.Horizontal;
scrollRect.vertical = ScrollDirection == Direction.Vertical;
RectTransform contentRectTransform = scrollRect.content.GetComponent<RectTransform>();
if (ScrollDirection == Direction.Vertical)
{
contentRectTransform.anchorMin = Vector2.up;
contentRectTransform.anchorMax = Vector2.one;
}
else if (ScrollDirection == Direction.Horizontal)
{
contentRectTransform.anchorMin = Vector2.zero;
contentRectTransform.anchorMax = Vector2.up;
}
contentRectTransform.anchoredPosition = Vector2.zero;
contentRectTransform.sizeDelta = Vector2.zero;
scrollRect.onValueChanged.AddListener(OnScrolled);
}
public void Nevigate(int index)
{
if(index < 0 || index >= itemDataList.Count)
return;
Vector2 target;
if (ScrollDirection == Direction.Vertical)
{
float offset = (index - 1) * (GetItemSize() + Spacing) + GetItemSize() * (1 - itemObject.GetComponent<RectTransform>().pivot.y);
offset -= scrollRect.viewport.rect.height * 0.5f;
target = new Vector2(0.0f, Mathf.Clamp01(1 - offset / (scrollRect.content.rect.height - scrollRect.viewport.rect.height)));
}
else
{
float offset = (index - 1) * (GetItemSize() + Spacing) + GetItemSize() * (1 - itemObject.GetComponent<RectTransform>().pivot.x);
offset -= scrollRect.viewport.rect.width * 0.5f;
target = new Vector2(Mathf.Clamp01(offset / (scrollRect.content.rect.width - scrollRect.viewport.rect.width)), 0.0f);
}
DOTween.To(() => scrollRect.normalizedPosition, x => scrollRect.normalizedPosition = x, target, 1.0f);
}
public void BuildContent(GameObject go, List<ReuseItemData> dataList, float spacing = 20.0f)
{
this.itemObject = go;
itemDataList = dataList;
Spacing = spacing;
ReloadData();
}
private void OnScrolled(Vector2 pos)
{
ReuseItems(pos - scrollPosition);
FillItems();
scrollPosition = pos;
}
private void ReloadData()
{
Vector2 sizeDelta = scrollRect.content.sizeDelta;
float contentSize = 0;
for (int i = 0; i < itemDataList.Count; i++)
{
contentSize += GetItemSize() + (i > 0 ? Spacing : 0);
}
if (ScrollDirection == Direction.Vertical)
{
sizeDelta.y = contentSize > rectTransform.rect.height ? contentSize : rectTransform.rect.height;
}
else if (ScrollDirection == Direction.Horizontal)
{
sizeDelta.x = contentSize > rectTransform.rect.width ? contentSize : rectTransform.rect.width;
}
scrollRect.content.sizeDelta = sizeDelta;
foreach (ReuseItem cell in itemsLinkedList)
{
Destroy(cell.gameObject);
}
itemsLinkedList.Clear();
scrollRect.normalizedPosition = scrollRect.content.GetComponent<RectTransform>().anchorMin;
scrollRect.onValueChanged.Invoke(scrollRect.normalizedPosition);
}
private void CreateItem(int index)
{
ReuseItem reuseItem = Instantiate(itemObject).GetComponent<ReuseItem>();
reuseItem.SetAnchors(scrollRect.content.anchorMin, scrollRect.content.anchorMax);
reuseItem.transform.SetParent(scrollRect.content.transform, false);
UpdateItem(reuseItem, index);
if (ScrollDirection == Direction.Vertical)
{
reuseItem.Top = (itemsLinkedList.Count > 0 ? itemsLinkedList.Last.Value.Bottom - Spacing : 0);
reuseItem.SetOffsetHorizontal(0, 0);
}
else if (ScrollDirection == Direction.Horizontal)
{
reuseItem.Left = (itemsLinkedList.Count > 0 ? itemsLinkedList.Last.Value.Right + Spacing : 0);
reuseItem.SetOffsetVertical(0, 0);
}
itemsLinkedList.AddLast(reuseItem);
}
private void UpdateItem(ReuseItem cell, int index)
{
cell.DataIndex = index;
if (cell.DataIndex >= 0 && cell.DataIndex < itemDataList.Count)
{
cell.UpdateSetDataToItem(itemDataList[cell.DataIndex]);
cell.gameObject.SetActive(true);
}
else
{
cell.gameObject.SetActive(false);
}
}
private void FillItems()
{
if (itemsLinkedList.Count == 0)
CreateItem(0);
while (CellsTailEdge + Spacing <= ActiveTailEdge)
{
CreateItem(itemsLinkedList.Last.Value.DataIndex + 1);
}
}
private void ReuseItems(Vector2 scrollVector)
{
if (itemsLinkedList.Count == 0)
return;
if (ScrollDirection == Direction.Vertical)
{
if (scrollVector.y > 0)
{
while (CellsTailEdge - GetItemSize() >= ActiveTailEdge)
{
MoveCellLastToFirst();
}
}
else if (scrollVector.y < 0)
{
while (CellsHeadEdge + GetItemSize() <= ActiveHeadEdge)
{
MoveCellFirstToLast();
}
}
}
else if (ScrollDirection == Direction.Horizontal)
{
if (scrollVector.x > 0)
{
while (CellsHeadEdge + GetItemSize() <= ActiveHeadEdge)
{
MoveCellFirstToLast();
}
}
else if (scrollVector.x < 0)
{
while (CellsTailEdge - GetItemSize() >= ActiveTailEdge)
{
MoveCellLastToFirst();
}
}
}
}
private void MoveCellFirstToLast()
{
if (itemsLinkedList.Count == 0) return;
ReuseItem firstCell = itemsLinkedList.First.Value;
ReuseItem lastCell = itemsLinkedList.Last.Value;
UpdateItem(firstCell, lastCell.DataIndex + 1);
if (ScrollDirection == Direction.Vertical)
{
firstCell.Top = lastCell.Bottom - Spacing;
firstCell.SetOffsetHorizontal(0, 0);
}
else if (ScrollDirection == Direction.Horizontal)
{
firstCell.Left = lastCell.Right + Spacing;
firstCell.SetOffsetVertical(0, 0);
}
itemsLinkedList.RemoveFirst();
itemsLinkedList.AddLast(firstCell);
}
private void MoveCellLastToFirst()
{
if (itemsLinkedList.Count == 0) return;
ReuseItem lastCell = itemsLinkedList.Last.Value;
ReuseItem firstCell = itemsLinkedList.First.Value;
UpdateItem(lastCell, firstCell.DataIndex - 1);
if (ScrollDirection == Direction.Vertical)
{
lastCell.Bottom = firstCell.Top + Spacing;
lastCell.SetOffsetHorizontal(0, 0);
}
else if (ScrollDirection == Direction.Horizontal)
{
lastCell.Right = firstCell.Left - Spacing;
lastCell.SetOffsetVertical(0, 0);
}
itemsLinkedList.RemoveLast();
itemsLinkedList.AddFirst(lastCell);
}
private float GetItemSize()
{
if (ScrollDirection == Direction.Vertical)
return itemObject.GetComponent<RectTransform>().rect.height;
else
return itemObject.GetComponent<RectTransform>().rect.width;
}
private float ActiveHeadEdge
{
get
{
if (ScrollDirection == Direction.Vertical)
{
return scrollRect.content.anchoredPosition.y;
}
else if (ScrollDirection == Direction.Horizontal)
{
return -scrollRect.content.anchoredPosition.x;
}
return 0;
}
}
private float ActiveTailEdge
{
get
{
if (ScrollDirection == Direction.Vertical)
{
return scrollRect.content.anchoredPosition.y + rectTransform.rect.height;
}
else if (ScrollDirection == Direction.Horizontal)
{
return -scrollRect.content.anchoredPosition.x + rectTransform.rect.width;
}
return 0;
}
}
private float CellsHeadEdge
{
get
{
if (ScrollDirection == Direction.Vertical)
{
return itemsLinkedList.Count > 0 ? -itemsLinkedList.First.Value.Top : 0;
}
else if (ScrollDirection == Direction.Horizontal)
{
return itemsLinkedList.Count > 0 ? itemsLinkedList.First.Value.Left : 0;
}
return 0;
}
}
private float CellsTailEdge
{
get
{
if (ScrollDirection == Direction.Vertical)
{
return itemsLinkedList.Count > 0 ? -itemsLinkedList.Last.Value.Bottom : 0;
}
else if (ScrollDirection == Direction.Horizontal)
{
return itemsLinkedList.Count > 0 ? itemsLinkedList.Last.Value.Right : 0;
}
return 0;
}
}
}