4using System.Collections.Generic;
16 public abstract class FancyScrollView<TItemData, TContext> : MonoBehaviour where TContext : class, new()
21 [SerializeField, Range(1e-2f, 1f)]
protected float cellInterval = 0.2f;
29 [SerializeField, Range(0f, 1f)]
protected float scrollOffset = 0.5f;
38 [SerializeField]
protected bool loop =
false;
45 readonly IList<FancyCell<TItemData, TContext>> pool =
new List<FancyCell<TItemData, TContext>>();
65 protected IList<TItemData>
ItemsSource {
get;
set; } =
new List<TItemData>();
71 protected TContext
Context {
get; } =
new TContext();
118 var firstIndex = Mathf.CeilToInt(p);
123 ResizePool(firstPosition);
126 UpdateCells(firstPosition, firstIndex, forceRefresh);
129 void ResizePool(
float firstPosition)
134 var addCount = Mathf.CeilToInt((1f - firstPosition) /
cellInterval) - pool.Count;
135 for (var i = 0; i < addCount; i++)
140 throw new MissingComponentException(
string.Format(
141 "FancyCell<{0}, {1}> component not found in {2}.",
142 typeof(TItemData).FullName, typeof(TContext).FullName,
CellPrefab.name));
147 cell.SetVisible(
false);
152 void UpdateCells(
float firstPosition,
int firstIndex,
bool forceRefresh)
154 for (var i = 0; i < pool.Count; i++)
156 var index = firstIndex + i;
158 var cell = pool[CircularIndex(index, pool.Count)];
165 if (index < 0 || index >=
ItemsSource.Count || position > 1f)
167 cell.SetVisible(
false);
171 if (forceRefresh || cell.Index != index || !cell.IsVisible)
174 cell.SetVisible(
true);
178 cell.UpdatePosition(position);
182 int CircularIndex(
int i,
int size) => size < 1 ? 0 : i < 0 ? size - 1 + (i + 1) % size : i % size;
186 float cachedCellInterval, cachedScrollOffset;
190 if (cachedLoop !=
loop ||
FancyScrollView<TItemData> のコンテキストクラス.
Credit Erdener Gonenc - @PixelEnvision.