FancyCellGroup.cs 2.5 KB

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