Skip to content
This repository was archived by the owner on Mar 20, 2023. It is now read-only.

Commit 9a66b92

Browse files
Merged in dev/fancyscrollviewupdate (pull request #77)
Updated Fancy Scroll View code to the latest from repo
2 parents 1fc8bba + de989b3 commit 9a66b92

20 files changed

+411
-277
lines changed

Runtime/Scripts/Layout/FancyScrollView/Core/FancyScrollViewCell.cs renamed to Runtime/Scripts/Layout/FancyScrollView/Core/FancyCell.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
/// Credit setchi (https://github.com/setchi)
22
/// Sourced from - https://github.com/setchi/FancyScrollView
33

4+
45
namespace UnityEngine.UI.Extensions
56
{
67
/// <summary>
78
/// <see cref="FancyScrollView{TItemData, TContext}"/> のセルを実装するための抽象基底クラス.
8-
/// <see cref="FancyScrollViewCell{TItemData, TContext}.Context"/> が不要な場合は
9-
/// 代わりに <see cref="FancyScrollViewCell{TItemData}"/> を使用します.
9+
/// <see cref="FancyCell{TItemData, TContext}.Context"/> が不要な場合は
10+
/// 代わりに <see cref="FancyCell{TItemData}"/> を使用します.
1011
/// </summary>
1112
/// <typeparam name="TItemData">アイテムのデータ型.</typeparam>
1213
/// <typeparam name="TContext"><see cref="Context"/> の型.</typeparam>
13-
public abstract class FancyScrollViewCell<TItemData, TContext> : MonoBehaviour where TContext : class, new()
14+
public abstract class FancyCell<TItemData, TContext> : MonoBehaviour where TContext : class, new()
1415
{
1516
/// <summary>
1617
/// このセルで表示しているデータのインデックス.
@@ -29,10 +30,15 @@ namespace UnityEngine.UI.Extensions
2930
protected TContext Context { get; private set; }
3031

3132
/// <summary>
32-
/// <see cref="Context"/> のセットアップを行います.
33+
/// <see cref="Context"/> をセットします.
3334
/// </summary>
3435
/// <param name="context">コンテキスト.</param>
35-
public virtual void SetupContext(TContext context) => Context = context;
36+
public virtual void SetContext(TContext context) => Context = context;
37+
38+
/// <summary>
39+
/// 初期化を行います.
40+
/// </summary>
41+
public virtual void Initialize() { }
3642

3743
/// <summary>
3844
/// このセルの可視状態を設定します.
@@ -57,10 +63,10 @@ namespace UnityEngine.UI.Extensions
5763
/// <see cref="FancyScrollView{TItemData}"/> のセルを実装するための抽象基底クラス.
5864
/// </summary>
5965
/// <typeparam name="TItemData">アイテムのデータ型.</typeparam>
60-
/// <seealso cref="FancyScrollViewCell{TItemData, TContext}"/>
61-
public abstract class FancyScrollViewCell<TItemData> : FancyScrollViewCell<TItemData, FancyScrollViewNullContext>
66+
/// <seealso cref="FancyCell{TItemData, TContext}"/>
67+
public abstract class FancyCell<TItemData> : FancyCell<TItemData, NullContext>
6268
{
6369
/// <inheritdoc/>
64-
public sealed override void SetupContext(FancyScrollViewNullContext context) => base.SetupContext(context);
70+
public sealed override void SetContext(NullContext context) => base.SetContext(context);
6571
}
6672
}

Runtime/Scripts/Layout/FancyScrollView/Core/FancyScrollViewCell.cs.meta renamed to Runtime/Scripts/Layout/FancyScrollView/Core/FancyCell.cs.meta

File renamed without changes.

Runtime/Scripts/Layout/FancyScrollView/Core/FancyScrollView.cs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ namespace UnityEngine.UI.Extensions
4242
/// </summary>
4343
[SerializeField] protected Transform cellContainer = default;
4444

45-
readonly IList<FancyScrollViewCell<TItemData, TContext>> pool =
46-
new List<FancyScrollViewCell<TItemData, TContext>>();
45+
readonly IList<FancyCell<TItemData, TContext>> pool = new List<FancyCell<TItemData, TContext>>();
4746

4847
/// <summary>
4948
/// 初期化済みかどうか.
@@ -90,7 +89,12 @@ protected virtual void UpdateContents(IList<TItemData> itemsSource)
9089
}
9190

9291
/// <summary>
93-
/// セルの表示内容を更新します.
92+
/// セルのレイアウトを強制的に更新します.
93+
/// </summary>
94+
protected virtual void Relayout() => UpdatePosition(currentPosition, false);
95+
96+
/// <summary>
97+
/// セルのレイアウトと表示内容を強制的に更新します.
9498
/// </summary>
9599
protected virtual void Refresh() => UpdatePosition(currentPosition, true);
96100

@@ -130,16 +134,16 @@ void ResizePool(float firstPosition)
130134
var addCount = Mathf.CeilToInt((1f - firstPosition) / cellInterval) - pool.Count;
131135
for (var i = 0; i < addCount; i++)
132136
{
133-
var cell = Instantiate(CellPrefab, cellContainer)
134-
.GetComponent<FancyScrollViewCell<TItemData, TContext>>();
137+
var cell = Instantiate(CellPrefab, cellContainer).GetComponent<FancyCell<TItemData, TContext>>();
135138
if (cell == null)
136139
{
137-
throw new MissingComponentException(
138-
$"FancyScrollViewCell<{typeof(TItemData).FullName}, {typeof(TContext).FullName}> " +
139-
$"component not found in {CellPrefab.name}.");
140+
throw new MissingComponentException(string.Format(
141+
"FancyCell<{0}, {1}> component not found in {2}.",
142+
typeof(TItemData).FullName, typeof(TContext).FullName, CellPrefab.name));
140143
}
141144

142-
cell.SetupContext(Context);
145+
cell.SetContext(Context);
146+
cell.Initialize();
143147
cell.SetVisible(false);
144148
pool.Add(cell);
145149
}
@@ -200,13 +204,13 @@ void LateUpdate()
200204
/// <summary>
201205
/// <see cref="FancyScrollView{TItemData}"/> のコンテキストクラス.
202206
/// </summary>
203-
public sealed class FancyScrollViewNullContext { }
207+
public sealed class NullContext { }
204208

205209
/// <summary>
206210
/// スクロールビューを実装するための抽象基底クラス.
207211
/// 無限スクロールおよびスナップに対応しています.
208212
/// </summary>
209213
/// <typeparam name="TItemData"></typeparam>
210214
/// <seealso cref="FancyScrollView{TItemData, TContext}"/>
211-
public abstract class FancyScrollView<TItemData> : FancyScrollView<TItemData, FancyScrollViewNullContext> { }
215+
public abstract class FancyScrollView<TItemData> : FancyScrollView<TItemData, NullContext> { }
212216
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/// Credit setchi (https://github.com/setchi)
2+
/// Sourced from - https://github.com/setchi/FancyScrollView
3+
4+
using System.Linq;
5+
6+
namespace UnityEngine.UI.Extensions
7+
{
8+
/// <summary>
9+
/// 複数の <see cref="FancyCell{TItemData, TContext}"/> を持つセルグループ実装するための抽象基底クラス.
10+
/// </summary>
11+
/// <typeparam name="TItemData">アイテムのデータ型.</typeparam>
12+
/// <typeparam name="TContext"><see cref="FancyCell{TItemData, TContext}.Context"/> の型.</typeparam>
13+
public abstract class FancyCellGroup<TItemData, TContext> : FancyCell<TItemData[], TContext>
14+
where TContext : class, IFancyCellGroupContext, new()
15+
{
16+
/// <summary>
17+
/// このグループで表示するセルの配列.
18+
/// </summary>
19+
protected virtual FancyCell<TItemData, TContext>[] Cells { get; private set; }
20+
21+
/// <summary>
22+
/// このグループで表示するセルの配列をインスタンス化します.
23+
/// </summary>
24+
/// <returns>このグループで表示するセルの配列.</returns>
25+
protected virtual FancyCell<TItemData, TContext>[] InstantiateCells()
26+
{
27+
return Enumerable.Range(0, Context.GetGroupCount())
28+
.Select(_ => Instantiate(Context.CellTemplate, transform))
29+
.Select(x => x.GetComponent<FancyCell<TItemData, TContext>>())
30+
.ToArray();
31+
}
32+
33+
/// <inheritdoc/>
34+
public override void Initialize()
35+
{
36+
Cells = InstantiateCells();
37+
Debug.Assert(Cells.Length == Context.GetGroupCount());
38+
39+
for (var i = 0; i < Cells.Length; i++)
40+
{
41+
Cells[i].SetContext(Context);
42+
Cells[i].Initialize();
43+
}
44+
}
45+
46+
/// <inheritdoc/>
47+
public override void UpdateContent(TItemData[] contents)
48+
{
49+
var firstCellIndex = Index * Context.GetGroupCount();
50+
51+
for (var i = 0; i < Cells.Length; i++)
52+
{
53+
Cells[i].Index = i + firstCellIndex;
54+
Cells[i].SetVisible(i < contents.Length);
55+
56+
if (Cells[i].IsVisible)
57+
{
58+
Cells[i].UpdateContent(contents[i]);
59+
}
60+
}
61+
}
62+
63+
/// <inheritdoc/>
64+
public override void UpdatePosition(float position)
65+
{
66+
for (var i = 0; i < Cells.Length; i++)
67+
{
68+
Cells[i].UpdatePosition(position);
69+
}
70+
}
71+
}
72+
}

Runtime/Scripts/Layout/FancyScrollView/ScrollRect/Alignment.cs.meta renamed to Runtime/Scripts/Layout/FancyScrollView/GridView/FancyCellGroup.cs.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)