切换标签
This commit is contained in:
69
Assets/Scripts/UI/Common/ClassifyList.cs
Normal file
69
Assets/Scripts/UI/Common/ClassifyList.cs
Normal file
@@ -0,0 +1,69 @@
|
||||
// 本脚本只在不存在时会生成一次。组件逻辑写在当前脚本内。已存在不会再次生成覆盖
|
||||
|
||||
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;
|
||||
|
||||
public int SelectedIndex => List.selectedIndex;
|
||||
|
||||
private void OnInited()
|
||||
{
|
||||
List.itemProvider = GetListItemResource;
|
||||
List.itemRenderer = OnRenderItem;
|
||||
List.onClickItem.Add(OnClickListItem);
|
||||
}
|
||||
|
||||
public void SetListData(List<object> listData)
|
||||
{
|
||||
_listData.Clear();
|
||||
foreach (var obj in listData)
|
||||
{
|
||||
_listData.Add(obj);
|
||||
}
|
||||
List.numItems = _listData.Count;
|
||||
List.ScrollToView(0);
|
||||
}
|
||||
|
||||
void OnClickListItem(EventContext context)
|
||||
{
|
||||
Debug.Log($"con={context.data} nm={context.sender}");
|
||||
OnClickItem?.Invoke(null);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user