62 lines
2.2 KiB
C#
62 lines
2.2 KiB
C#
using System.Collections.Generic;
|
|
using EnhancedUI.EnhancedScroller;
|
|
using TMPro;
|
|
using UI.PartnerGather.ScrollItems;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using ScrollViewItemInfo = EventPartnerScrollViewItem.ScrollViewItemInfo;
|
|
|
|
public class EventPartnerScrollViewTitle : EnhancedScrollerCellView
|
|
{
|
|
public TMP_Text textTitle;
|
|
[SerializeField] private RectTransform itemContainer;
|
|
[SerializeField] private EventPartnerGatherScrollViewItem itemPrefab;
|
|
|
|
private readonly List<EventPartnerGatherScrollViewItem> _itemViews = new();
|
|
|
|
/// <param name="itemHeight">单行 Item 高度,须与邀请面板 jiandaItemHeight 一致。</param>
|
|
/// <param name="itemSpacing">行间距,须与邀请面板 sectionItemSpacing 及 GetCellViewSize 中 Section 高度公式一致。</param>
|
|
public void Init(ScrollViewItemInfo sectionInfo, float itemHeight, float itemSpacing = 0f)
|
|
{
|
|
textTitle.text = sectionInfo.Title;
|
|
|
|
var items = sectionInfo.SectionItems;
|
|
var count = items?.Count ?? 0;
|
|
|
|
while (_itemViews.Count < count)
|
|
{
|
|
var item = Instantiate(itemPrefab, itemContainer);
|
|
var rt = item.transform as RectTransform;
|
|
rt.anchorMin = new Vector2(0, 1);
|
|
rt.anchorMax = new Vector2(1, 1);
|
|
rt.pivot = new Vector2(0.5f, 1);
|
|
_itemViews.Add(item);
|
|
}
|
|
|
|
var stride = itemHeight + itemSpacing;
|
|
for (var i = 0; i < _itemViews.Count; i++)
|
|
{
|
|
if (i < count)
|
|
{
|
|
_itemViews[i].gameObject.SetActive(true);
|
|
_itemViews[i].Init(items[i]);
|
|
var rt = _itemViews[i].transform as RectTransform;
|
|
rt.anchoredPosition = new Vector2(0, -i * stride);
|
|
rt.sizeDelta = new Vector2(0, itemHeight);
|
|
}
|
|
else
|
|
{
|
|
_itemViews[i].gameObject.SetActive(false);
|
|
}
|
|
}
|
|
|
|
if (itemContainer != null)
|
|
{
|
|
var contentH = count <= 0 ? 0f : count * itemHeight + (count - 1) * itemSpacing;
|
|
var w = itemContainer.sizeDelta.x;
|
|
itemContainer.sizeDelta = new Vector2(w, contentH);
|
|
LayoutRebuilder.ForceRebuildLayoutImmediate(itemContainer);
|
|
}
|
|
}
|
|
}
|