Files
Fishing2/Assets/Scripts/UI/Common/ClassifyList.cs
2025-05-23 18:10:56 +08:00

68 lines
1.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 本脚本只在不存在时会生成一次。组件逻辑写在当前脚本内。已存在不会再次生成覆盖
using System;
using System.Collections.Generic;
using UnityEngine;
using FairyGUI;
using NBC;
namespace NBF
{
public partial class ClassifyList : GComponent
{
private readonly List<object> _listData = new List<object>();
public event Action<object> OnClickItem;
private void OnInited()
{
List.itemProvider = GetListItemResource;
List.itemRenderer = OnRenderItem;
List.onClickItem.Add(OnClickListItem);
}
public void SetListData(List<object> listData,ListSelectionMode selectionMode = ListSelectionMode.Single)
{
_listData.Clear();
foreach (var obj in listData)
{
_listData.Add(obj);
}
List.selectionMode = selectionMode;
List.numItems = _listData.Count;
List.ScrollToView(0);
}
void OnClickListItem(EventContext context)
{
OnClickItem?.Invoke(context.data);
}
void OnRenderItem(int index, GObject obj)
{
if (obj is ListItemBase item)
{
item.SetData(_listData[index]);
}
}
//根据索引的不同返回不同的资源URL
string GetListItemResource(int index)
{
var itemData = _listData[index];
if (itemData is ShopGearData shopItem)
{
return ShopGearItem.URL;
}
if (itemData is ListClassifyData item)
{
return ListTitleItem.URL;
}
return List.defaultItem;
}
}
}