Skip to content

Commit dd64641

Browse files
committed
循环列表、循环折叠列表功能完成
1 parent dc7d600 commit dd64641

File tree

5 files changed

+87
-95
lines changed

5 files changed

+87
-95
lines changed
1.91 KB
Binary file not shown.

Assets/Scripts/TestCircularScrollView/Test.cs renamed to Assets/Scripts/TestCircularScrollView/TestCircularScrollView.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,23 @@
22
using UnityEngine.Events;
33
using UnityEngine.UI;
44

5-
public class Test : MonoBehaviour
5+
public class TestCircularScrollView : MonoBehaviour
66
{
77
public UICircularScrollView _circularH;
88
public UICircularScrollView _circularV;
9-
public UICircularFlodScrollView _flodCircular;
9+
public UICircularFlodScrollView _flodCircularH;
10+
public UICircularFlodScrollView _flodCircularV;
1011

1112
void Start()
1213
{
1314
_circularH.Init(CellCallBack);
1415
_circularH.ShowList(1000);
1516
_circularV.Init(CellCallBack);
1617
_circularV.ShowList(1000);
17-
_flodCircular.Init(FlodBtnCallback, FlodCellCallback);
18-
_flodCircular.ShowList(2, 3, 4, 5, 6, 7, 8);
18+
_flodCircularH.Init(FlodBtnCallback, FlodCellCallback);
19+
_flodCircularH.ShowList(2, 3, 4, 5, 6, 7, 8);
20+
_flodCircularV.Init(FlodBtnCallback, FlodCellCallback);
21+
_flodCircularV.ShowList(2, 3, 4, 5, 6, 7, 8);
1922
}
2023

2124
private void CellCallBack(GameObject cell, int index)

Assets/Scripts/TestCircularScrollView/Test.cs.meta renamed to Assets/Scripts/TestCircularScrollView/TestCircularScrollView.cs.meta

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Scripts/TestCircularScrollView/UICircularFlodScrollView.cs

Lines changed: 63 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,16 @@ public class UICircularFlodScrollView : UICircularScrollView
1818
/// </summary>
1919
private struct FlodBtnInfo
2020
{
21-
public int index;
2221
public GameObject obj;
2322
public bool isFlod;
2423
public CellInfo[] cellInfos;
2524
public float cellsSize;//子元素横向或纵尺寸
26-
public int cellCount;
2725
}
2826
private FlodBtnInfo[] _flodBtnInfos;
2927

3028
private UnityAction<GameObject, int> OnFlodBtnShow;
3129
private UnityAction<GameObject, int, int> OnFlodCellShow;
32-
private UnityAction<int, bool, GameObject> OnFlodBtnClick;
30+
private UnityAction<GameObject, int, bool> OnFlodBtnClick;
3331

3432
public void Init(UnityAction<GameObject, int> onFlodBtnShow, UnityAction<GameObject, int, int> onFlodCellShow)
3533
{
@@ -54,49 +52,43 @@ public void Init(UnityAction<GameObject, int> onFlodBtnShow, UnityAction<GameObj
5452
public void ShowList(params int[] num)
5553
{
5654
RecycleItems();
57-
int curCellCount = 0;//当前元素数量
58-
int cellCount = 0;//元素总数量
59-
int flodBtnCount = num.Length;//折叠按钮总数量
55+
int flodBtnCount = num.Length;
6056
_flodBtnInfos = new FlodBtnInfo[flodBtnCount];
61-
for (int k = 0; k < flodBtnCount; k++)
57+
for (int i = 0; i < flodBtnCount; i++)
6258
{
6359
//====生成折叠按钮
6460
GameObject flodObj = ObjectPool.Instance.GetObject(FLODBTNPOOL, _content);
6561
flodObj.SetActive(true);
66-
flodObj.name = k.ToString();
67-
int count = num[k];
68-
cellCount += count;
62+
flodObj.name = i.ToString();
63+
int count = num[i];
6964
//存储按钮数据
7065
FlodBtnInfo flodBtnInfo = new FlodBtnInfo
7166
{
72-
index = k,
7367
obj = flodObj,
74-
cellCount = count,
7568
cellInfos = new CellInfo[count],
7669
isFlod = _defaultFlod,
7770
cellsSize = _dir == UIDir.Vertical ? (_cellHeight + _spacing) * Mathf.CeilToInt((float)count / _crNum) : (_cellWight + _spacing) * Mathf.CeilToInt((float)count / _crNum)
7871
};
79-
_flodBtnInfos[k] = flodBtnInfo;
80-
Button btn = flodObj.GetComponent<Button>();
81-
btn.onClick.AddListener(() => OnFlodClick(flodBtnInfo));
82-
SetFlodBtnPos(flodBtnInfo, curCellCount);
72+
_flodBtnInfos[i] = flodBtnInfo;
73+
int flodIndex = i;
74+
flodObj.GetComponent<Button>().onClick.AddListener(() => OnFlodClick(flodBtnInfo, flodIndex));
75+
SetFlodBtnPos(i);
8376
//=====生成cell
84-
for (int i = 0; i < count; i++)
77+
for (int k = 0; k < count; k++)
8578
{
8679
if (flodBtnInfo.isFlod) break;
8780
CellInfo cellInfo = new CellInfo();
88-
SetCellPos(ref cellInfo, k, i, curCellCount);
81+
SetCellPos(ref cellInfo, i, k);
8982
if (SetCellState(ref cellInfo))
9083
{
91-
cellInfo.obj.name = k + "-" + i;
92-
OnFlodCellShow(cellInfo.obj, i, k);//cell显示回调
84+
cellInfo.obj.name = i + "-" + k;
85+
OnFlodCellShow(cellInfo.obj, k, i);//cell显示回调
9386
}
94-
flodBtnInfo.cellInfos[i] = cellInfo;
87+
flodBtnInfo.cellInfos[k] = cellInfo;
9588
}
96-
curCellCount += flodBtnInfo.isFlod ? 0 : count;
97-
OnFlodBtnShow(flodObj, k);//flodBtn显示回调
89+
OnFlodBtnShow(flodObj, i);//flodBtn显示回调
9890
}
99-
SetContentSize(flodBtnCount, cellCount);
91+
SetContentSize(flodBtnCount);
10092
}
10193

10294
protected override void ScrollRectListener(Vector2 value)
@@ -106,7 +98,7 @@ protected override void ScrollRectListener(Vector2 value)
10698
{
10799
FlodBtnInfo flodBtnInfo = _flodBtnInfos[i];
108100
if (flodBtnInfo.isFlod) continue;
109-
for (int k = 0; k < flodBtnInfo.cellCount; k++)
101+
for (int k = 0; k < flodBtnInfo.cellInfos.Length; k++)
110102
{
111103
CellInfo cellInfo = flodBtnInfo.cellInfos[k];
112104
if (SetCellState(ref cellInfo))
@@ -119,6 +111,23 @@ protected override void ScrollRectListener(Vector2 value)
119111
}
120112
}
121113

114+
protected override void SetContentSize(int flodBtnCount)
115+
{
116+
int lastIndex = flodBtnCount - 1;
117+
if (_dir == UIDir.Vertical)
118+
{
119+
float sizeY = -_flodBtnInfos[lastIndex].obj.transform.localPosition.y + _flodBtnHeight + _spacing;
120+
sizeY += _defaultFlod ? 0 : _flodBtnInfos[lastIndex].cellsSize;
121+
_content.sizeDelta = new Vector2(_content.sizeDelta.x, sizeY);
122+
}
123+
else
124+
{
125+
float sizeX = _flodBtnInfos[lastIndex].obj.transform.localPosition.x + _flodBtnWidth + _spacing;
126+
sizeX += _defaultFlod ? 0 : _flodBtnInfos[lastIndex].cellsSize;
127+
_content.sizeDelta = new Vector2(sizeX, _content.sizeDelta.y);
128+
}
129+
}
130+
122131
private void RecycleItems()
123132
{
124133
if (!_isInited) return;
@@ -140,62 +149,47 @@ private void RecycleItems()
140149
}
141150
}
142151

143-
private void SetFlodBtnPos(FlodBtnInfo flod, int curCellCount)
152+
private void SetFlodBtnPos(int index)
144153
{
145154
if (_dir == UIDir.Vertical)
146155
{
147-
float pos = _spacing;
148-
if (flod.index > 0)
156+
float posY = -_spacing;
157+
if (index > 0)
149158
{
150-
pos += _cellHeight;
151-
pos += Mathf.Abs(_flodBtnInfos[flod.index - 1].obj.transform.localPosition.y);
152-
pos += _flodBtnInfos[flod.index - 1].isFlod ? 0 : _flodBtnInfos[flod.index - 1].cellsSize;
159+
posY -= _cellHeight;
160+
posY += _flodBtnInfos[index - 1].obj.transform.localPosition.y;
161+
posY -= _flodBtnInfos[index - 1].isFlod ? 0 : _flodBtnInfos[index - 1].cellsSize;
153162
}
154-
flod.obj.transform.localPosition = new Vector2(_flodBtnX, -pos);
163+
_flodBtnInfos[index].obj.transform.localPosition = new Vector2(_flodBtnX, posY);
155164
}
156165
else
157166
{
158-
float pos = _flodBtnWidth * flod.index + _spacing * (flod.index + 1);
159-
pos += flod.index > 0 ? (_cellWight + _spacing) * Mathf.CeilToInt((float)curCellCount / _crNum) : 0;
160-
flod.obj.transform.localPosition = new Vector2(pos, _flodBtnY);
167+
float posX = _spacing;
168+
if (index > 0)
169+
{
170+
posX += _cellWight;
171+
posX += _flodBtnInfos[index - 1].obj.transform.localPosition.x;
172+
posX += _flodBtnInfos[index - 1].isFlod ? 0 : _flodBtnInfos[index - 1].cellsSize;
173+
}
174+
_flodBtnInfos[index].obj.transform.localPosition = new Vector2(posX, _flodBtnY);
161175
}
162176
}
163177

164-
private void SetCellPos(ref CellInfo cellInfo, int flodIndex, int cellIndex, int curCellCount)
178+
private void SetCellPos(ref CellInfo cellInfo, int flodIndex, int cellIndex)
165179
{
166180
if (_dir == UIDir.Vertical)
167181
{
168-
float posY = (_cellHeight + _spacing) * (Mathf.CeilToInt(cellIndex / _crNum) + 1) + Mathf.Abs(_flodBtnInfos[flodIndex].obj.transform.localPosition.y);
169-
//posY += (_flodBtnHeight + _spacing) * (flodIndex + 1);
170-
//posY += (_cellHeight + _spacing) * Mathf.CeilToInt((float)curCellCount / _crNum);
171-
//posY += Mathf.Abs(_flodBtnInfos[flodIndex].obj.transform.localPosition.y) + _cellHeight;
172182
float posX = (_cellWight + _spacing) * (cellIndex % _crNum);
173-
cellInfo.pos = new Vector2(posX, -posY);
174-
}
175-
else
176-
{
177-
float pos = _cellWight * Mathf.CeilToInt(cellIndex / _crNum) + _spacing * (Mathf.CeilToInt(cellIndex / _crNum) + 1);
178-
pos += (_flodBtnWidth + _spacing) * (flodIndex + 1);
179-
pos += (_cellHeight + _spacing) * Mathf.CeilToInt((float)curCellCount / _crNum);
180-
float posY = _cellHeight * (cellIndex % _crNum) + _spacing * (cellIndex % _crNum);
181-
cellInfo.pos = new Vector2(pos, -posY);
182-
}
183-
}
184-
185-
private void SetContentSize(int flodBtnCount, int cellCount)
186-
{
187-
if (_dir == UIDir.Vertical)
188-
{
189-
float contentSize = Mathf.Abs(_flodBtnInfos[flodBtnCount - 1].obj.transform.localPosition.y) + _flodBtnHeight + _spacing;
190-
// float contentSize = (_spacing + _flodBtnHeight) * flodBtnCount;
191-
contentSize += _defaultFlod ? 0 : _flodBtnInfos[flodBtnCount - 1].cellsSize;
192-
_content.sizeDelta = new Vector2(_content.sizeDelta.x, contentSize);
183+
float posY = -(_cellHeight + _spacing) * (Mathf.CeilToInt(cellIndex / _crNum) + 1);
184+
posY += _flodBtnInfos[flodIndex].obj.transform.localPosition.y;
185+
cellInfo.pos = new Vector2(posX, posY);
193186
}
194187
else
195188
{
196-
float contentSize = _defaultFlod ? 0 : (_spacing + _cellWight) * Mathf.CeilToInt((float)cellCount / _crNum);
197-
contentSize += (_spacing + _flodBtnWidth) * flodBtnCount;
198-
_content.sizeDelta = new Vector2(contentSize, _content.sizeDelta.y);
189+
float posX = (_cellWight + _spacing) * (Mathf.CeilToInt(cellIndex / _crNum) + 1);
190+
posX += _flodBtnInfos[flodIndex].obj.transform.localPosition.x;
191+
float posY = -(_cellHeight + _spacing) * (cellIndex % _crNum);
192+
cellInfo.pos = new Vector2(posX, posY);
199193
}
200194
}
201195

@@ -214,12 +208,12 @@ private void SetContentSize(FlodBtnInfo flod)
214208
}
215209
}
216210

217-
private void OnFlodClick(FlodBtnInfo flod)
211+
private void OnFlodClick(FlodBtnInfo flod,int index)
218212
{
219-
OnFlodClick(flod.index);
213+
OnFlodClick(index);
220214
if (OnFlodBtnClick != null)
221215
{
222-
OnFlodBtnClick(flod.index, flod.isFlod, flod.obj);
216+
OnFlodBtnClick(flod.obj, index, flod.isFlod);
223217
}
224218
}
225219

@@ -228,15 +222,14 @@ private void OnFlodClick(int index)
228222
_flodBtnInfos[index].isFlod = !_flodBtnInfos[index].isFlod;
229223
//重置Content大小
230224
SetContentSize(_flodBtnInfos[index]);
231-
int curCellCount = 0;
232225
//下面的折叠按钮,重新计算坐标显示处理
233226
for (int i = 0; i < _flodBtnInfos.Length; i++)
234227
{
235228
FlodBtnInfo flodBtnInfo = _flodBtnInfos[i];
236-
int count = flodBtnInfo.cellCount;
229+
int count = flodBtnInfo.cellInfos.Length;
237230
if (i >= index)
238231
{
239-
SetFlodBtnPos(_flodBtnInfos[i], curCellCount);
232+
SetFlodBtnPos(i);
240233
for (int k = 0; k < count; k++)
241234
{
242235
if (flodBtnInfo.isFlod)
@@ -250,7 +243,7 @@ private void OnFlodClick(int index)
250243
else
251244
{
252245
CellInfo cellInfo = flodBtnInfo.cellInfos[k];
253-
SetCellPos(ref cellInfo, i, k, curCellCount);
246+
SetCellPos(ref cellInfo, i, k);
254247
if (SetCellState(ref cellInfo))
255248
{
256249
OnFlodCellShow(cellInfo.obj, i, k);//cell显示回调
@@ -260,10 +253,6 @@ private void OnFlodClick(int index)
260253
}
261254
}
262255
}
263-
if (!_flodBtnInfos[i].isFlod)
264-
{
265-
curCellCount += count;
266-
}
267256
}
268257
}
269258
}

Assets/Scripts/TestCircularScrollView/UICircularScrollView.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class UICircularScrollView : MonoBehaviour
2323
/// </summary>
2424
protected struct CellInfo
2525
{
26-
public Vector3 pos;
26+
public Vector2 pos;
2727
public GameObject obj;
2828
}
2929
protected CellInfo[] _cellInfos;
@@ -112,22 +112,11 @@ protected virtual void ScrollRectListener(Vector2 value)
112112
}
113113
}
114114

115-
/// <summary>
116-
/// 设置RectTransform左上角锚点
117-
/// </summary>
118-
/// <param name="rectTrans"></param>
119-
protected void SetTopLeftAnchor(RectTransform rectTrans)
120-
{
121-
rectTrans.pivot = Vector2.up;
122-
rectTrans.anchorMin = Vector2.up;
123-
rectTrans.anchorMax = Vector2.up;
124-
}
125-
126115
/// <summary>
127116
/// 设置Content尺寸
128117
/// </summary>
129118
/// <param name="count"></param>
130-
protected void SetContentSize(int count)
119+
protected virtual void SetContentSize(int count)
131120
{
132121
if (_dir == UIDir.Vertical)
133122
{
@@ -147,6 +136,17 @@ protected void SetContentSize(int count)
147136
}
148137
}
149138

139+
/// <summary>
140+
/// 设置RectTransform左上角锚点
141+
/// </summary>
142+
/// <param name="rectTrans"></param>
143+
protected void SetTopLeftAnchor(RectTransform rectTrans)
144+
{
145+
rectTrans.pivot = Vector2.up;
146+
rectTrans.anchorMin = Vector2.up;
147+
rectTrans.anchorMax = Vector2.up;
148+
}
149+
150150
/// <summary>
151151
/// 回收CellInfos
152152
/// </summary>
@@ -180,13 +180,13 @@ protected void SetCellPos(ref CellInfo cellInfo, int index)
180180
{
181181
float posY = -(_cellHeight * Mathf.CeilToInt(index / _crNum) + _spacing * Mathf.CeilToInt(index / _crNum));
182182
float posX = _cellWight * (index % _crNum) + _spacing * (index % _crNum);
183-
cellInfo.pos = new Vector3(posX, posY, 0);
183+
cellInfo.pos = new Vector2(posX, posY);
184184
}
185185
else
186186
{
187187
float posX = _cellWight * Mathf.CeilToInt(index / _crNum) + _spacing * Mathf.CeilToInt(index / _crNum);
188188
float PosY = -(_cellHeight * (index % _crNum) + _spacing * (index % _crNum));
189-
cellInfo.pos = new Vector3(posX, PosY, 0);
189+
cellInfo.pos = new Vector2(posX, PosY);
190190
}
191191
}
192192

0 commit comments

Comments
 (0)