2019-09-27 20:30:11 +08:00
|
|
|
|
/// Credit setchi (https://github.com/setchi)
|
|
|
|
|
/// Sourced from - https://github.com/setchi/FancyScrollView
|
|
|
|
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
namespace UnityEngine.UI.Extensions
|
|
|
|
|
{
|
2019-12-05 01:25:31 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// スクロールビューを実装するための抽象基底クラス.
|
|
|
|
|
/// 無限スクロールおよびスナップに対応しています.
|
|
|
|
|
/// <see cref="FancyScrollView{TItemData, TContext}.Context"/> が不要な場合は
|
|
|
|
|
/// 代わりに <see cref="FancyScrollView{TItemData}"/> を使用します.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TItemData">アイテムのデータ型.</typeparam>
|
|
|
|
|
/// <typeparam name="TContext"><see cref="Context"/> の型.</typeparam>
|
2019-09-27 20:30:11 +08:00
|
|
|
|
public abstract class FancyScrollView<TItemData, TContext> : MonoBehaviour where TContext : class, new()
|
|
|
|
|
{
|
2019-12-05 01:25:31 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// セル同士の間隔.
|
|
|
|
|
/// </summary>
|
2019-11-16 22:49:21 +08:00
|
|
|
|
[SerializeField, Range(1e-2f, 1f)] protected float cellInterval = 0.2f;
|
2019-12-05 01:25:31 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// スクロール位置の基準.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// たとえば、 <c>0.5</c> を指定してスクロール位置が <c>0</c> の場合, 中央に最初のセルが配置されます.
|
|
|
|
|
/// </remarks>
|
2019-09-27 20:30:11 +08:00
|
|
|
|
[SerializeField, Range(0f, 1f)] protected float scrollOffset = 0.5f;
|
2019-12-05 01:25:31 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// セルを循環して配置させるどうか.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// <c>true</c> にすると最後のセルの後に最初のセル, 最初のセルの前に最後のセルが並ぶようになります.
|
|
|
|
|
/// 無限スクロールを実装する場合は <c>true</c> を指定します.
|
|
|
|
|
/// </remarks>
|
2019-09-27 20:30:11 +08:00
|
|
|
|
[SerializeField] protected bool loop = false;
|
2019-12-05 01:25:31 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// セルの親要素となる <c>Transform</c>.
|
|
|
|
|
/// </summary>
|
2019-09-27 20:30:11 +08:00
|
|
|
|
[SerializeField] protected Transform cellContainer = default;
|
|
|
|
|
|
2020-06-30 22:14:04 +08:00
|
|
|
|
readonly IList<FancyCell<TItemData, TContext>> pool = new List<FancyCell<TItemData, TContext>>();
|
2019-09-27 20:30:11 +08:00
|
|
|
|
|
2019-12-05 01:25:31 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 初期化済みかどうか.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected bool initialized;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 現在のスクロール位置.
|
|
|
|
|
/// </summary>
|
2019-11-16 22:49:21 +08:00
|
|
|
|
protected float currentPosition;
|
2019-09-27 20:30:11 +08:00
|
|
|
|
|
2019-12-05 01:25:31 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// セルの Prefab.
|
|
|
|
|
/// </summary>
|
2019-09-27 20:30:11 +08:00
|
|
|
|
protected abstract GameObject CellPrefab { get; }
|
2019-12-05 01:25:31 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// アイテム一覧のデータ.
|
|
|
|
|
/// </summary>
|
2019-09-27 20:30:11 +08:00
|
|
|
|
protected IList<TItemData> ItemsSource { get; set; } = new List<TItemData>();
|
2019-12-05 01:25:31 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <typeparamref name="TContext"/> のインスタンス.
|
|
|
|
|
/// セルとスクロールビュー間で同じインスタンスが共有されます. 情報の受け渡しや状態の保持に使用します.
|
|
|
|
|
/// </summary>
|
2019-09-27 20:30:11 +08:00
|
|
|
|
protected TContext Context { get; } = new TContext();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2019-12-05 01:25:31 +08:00
|
|
|
|
/// 初期化を行います.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// 最初にセルが生成される直前に呼び出されます.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
protected virtual void Initialize() { }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 渡されたアイテム一覧に基づいて表示内容を更新します.
|
2019-09-27 20:30:11 +08:00
|
|
|
|
/// </summary>
|
2019-12-05 01:25:31 +08:00
|
|
|
|
/// <param name="itemsSource">アイテム一覧.</param>
|
2019-11-16 22:49:21 +08:00
|
|
|
|
protected virtual void UpdateContents(IList<TItemData> itemsSource)
|
2019-09-27 20:30:11 +08:00
|
|
|
|
{
|
|
|
|
|
ItemsSource = itemsSource;
|
|
|
|
|
Refresh();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-06-30 22:14:04 +08:00
|
|
|
|
/// セルのレイアウトを強制的に更新します.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected virtual void Relayout() => UpdatePosition(currentPosition, false);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// セルのレイアウトと表示内容を強制的に更新します.
|
2019-09-27 20:30:11 +08:00
|
|
|
|
/// </summary>
|
2019-11-16 22:49:21 +08:00
|
|
|
|
protected virtual void Refresh() => UpdatePosition(currentPosition, true);
|
2019-09-27 20:30:11 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2019-12-05 01:25:31 +08:00
|
|
|
|
/// スクロール位置を更新します.
|
2019-09-27 20:30:11 +08:00
|
|
|
|
/// </summary>
|
2019-12-05 01:25:31 +08:00
|
|
|
|
/// <param name="position">スクロール位置.</param>
|
2019-11-16 22:49:21 +08:00
|
|
|
|
protected virtual void UpdatePosition(float position) => UpdatePosition(position, false);
|
2019-09-27 20:30:11 +08:00
|
|
|
|
|
2019-12-05 01:25:31 +08:00
|
|
|
|
void UpdatePosition(float position, bool forceRefresh)
|
2019-09-27 20:30:11 +08:00
|
|
|
|
{
|
2019-12-05 01:25:31 +08:00
|
|
|
|
if (!initialized)
|
|
|
|
|
{
|
|
|
|
|
Initialize();
|
|
|
|
|
initialized = true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-27 20:30:11 +08:00
|
|
|
|
currentPosition = position;
|
|
|
|
|
|
|
|
|
|
var p = position - scrollOffset / cellInterval;
|
|
|
|
|
var firstIndex = Mathf.CeilToInt(p);
|
|
|
|
|
var firstPosition = (Mathf.Ceil(p) - p) * cellInterval;
|
|
|
|
|
|
|
|
|
|
if (firstPosition + pool.Count * cellInterval < 1f)
|
|
|
|
|
{
|
|
|
|
|
ResizePool(firstPosition);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UpdateCells(firstPosition, firstIndex, forceRefresh);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ResizePool(float firstPosition)
|
|
|
|
|
{
|
2019-11-16 22:49:21 +08:00
|
|
|
|
Debug.Assert(CellPrefab != null);
|
|
|
|
|
Debug.Assert(cellContainer != null);
|
2019-09-27 20:30:11 +08:00
|
|
|
|
|
|
|
|
|
var addCount = Mathf.CeilToInt((1f - firstPosition) / cellInterval) - pool.Count;
|
|
|
|
|
for (var i = 0; i < addCount; i++)
|
|
|
|
|
{
|
2020-06-30 22:14:04 +08:00
|
|
|
|
var cell = Instantiate(CellPrefab, cellContainer).GetComponent<FancyCell<TItemData, TContext>>();
|
2019-09-27 20:30:11 +08:00
|
|
|
|
if (cell == null)
|
|
|
|
|
{
|
2020-06-30 22:14:04 +08:00
|
|
|
|
throw new MissingComponentException(string.Format(
|
|
|
|
|
"FancyCell<{0}, {1}> component not found in {2}.",
|
|
|
|
|
typeof(TItemData).FullName, typeof(TContext).FullName, CellPrefab.name));
|
2019-09-27 20:30:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-30 22:14:04 +08:00
|
|
|
|
cell.SetContext(Context);
|
|
|
|
|
cell.Initialize();
|
2019-09-27 20:30:11 +08:00
|
|
|
|
cell.SetVisible(false);
|
|
|
|
|
pool.Add(cell);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UpdateCells(float firstPosition, int firstIndex, bool forceRefresh)
|
|
|
|
|
{
|
|
|
|
|
for (var i = 0; i < pool.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
var index = firstIndex + i;
|
|
|
|
|
var position = firstPosition + i * cellInterval;
|
|
|
|
|
var cell = pool[CircularIndex(index, pool.Count)];
|
|
|
|
|
|
|
|
|
|
if (loop)
|
|
|
|
|
{
|
|
|
|
|
index = CircularIndex(index, ItemsSource.Count);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (index < 0 || index >= ItemsSource.Count || position > 1f)
|
|
|
|
|
{
|
|
|
|
|
cell.SetVisible(false);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (forceRefresh || cell.Index != index || !cell.IsVisible)
|
|
|
|
|
{
|
|
|
|
|
cell.Index = index;
|
|
|
|
|
cell.SetVisible(true);
|
|
|
|
|
cell.UpdateContent(ItemsSource[index]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cell.UpdatePosition(position);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int CircularIndex(int i, int size) => size < 1 ? 0 : i < 0 ? size - 1 + (i + 1) % size : i % size;
|
|
|
|
|
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
bool cachedLoop;
|
|
|
|
|
float cachedCellInterval, cachedScrollOffset;
|
|
|
|
|
|
|
|
|
|
void LateUpdate()
|
|
|
|
|
{
|
2019-12-05 01:25:31 +08:00
|
|
|
|
if (cachedLoop != loop ||
|
|
|
|
|
cachedCellInterval != cellInterval ||
|
|
|
|
|
cachedScrollOffset != scrollOffset)
|
2019-09-27 20:30:11 +08:00
|
|
|
|
{
|
|
|
|
|
cachedLoop = loop;
|
|
|
|
|
cachedCellInterval = cellInterval;
|
|
|
|
|
cachedScrollOffset = scrollOffset;
|
|
|
|
|
|
|
|
|
|
UpdatePosition(currentPosition);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-05 01:25:31 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// <see cref="FancyScrollView{TItemData}"/> のコンテキストクラス.
|
|
|
|
|
/// </summary>
|
2020-06-30 22:14:04 +08:00
|
|
|
|
public sealed class NullContext { }
|
2019-09-27 20:30:11 +08:00
|
|
|
|
|
2019-12-05 01:25:31 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// スクロールビューを実装するための抽象基底クラス.
|
|
|
|
|
/// 無限スクロールおよびスナップに対応しています.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TItemData"></typeparam>
|
|
|
|
|
/// <seealso cref="FancyScrollView{TItemData, TContext}"/>
|
2020-06-30 22:14:04 +08:00
|
|
|
|
public abstract class FancyScrollView<TItemData> : FancyScrollView<TItemData, NullContext> { }
|
2019-11-16 22:49:21 +08:00
|
|
|
|
}
|