Files
Fishing2/Assets/Scripts/UI/Common/ClassifyList.cs
2025-09-07 23:50:53 +08:00

111 lines
3.0 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 int _columnsCount;
private void OnInited()
{
Game.Input.OnUICanceled += OnUICanceled;
}
public override void Dispose()
{
Game.Input.OnUICanceled -= OnUICanceled;
base.Dispose();
}
private void OnUICanceled(string action)
{
if (action == InputDef.UI.Right)
{
SetListSelected(List.selectedIndex + 1);
}
else if (action == InputDef.UI.Left)
{
SetListSelected(List.selectedIndex - 1);
}
else if (action == InputDef.UI.Up)
{
SetListSelected(List.selectedIndex - _columnsCount);
}
else if (action == InputDef.UI.Down)
{
if (List.selectedIndex < 0)
{
SetListSelected(0);
}
else
{
SetListSelected(List.selectedIndex + _columnsCount);
}
}
}
private void SetListSelected(int selectedIndex)
{
if (selectedIndex >= 0 && selectedIndex < _listData.Count)
{
List.selectedIndex = selectedIndex;
List.ScrollToView(List.selectedIndex);
}
}
public void SetListData(List<object> listData, ListSelectionMode selectionMode = ListSelectionMode.Single)
{
List.selectedIndex = -1;
List.defaultItem = GetListDefaultItemResource(listData);
List.itemRenderer = OnRenderItem;
List.onClickItem.Add(OnClickListItem);
List.SetVirtual();
_listData.Clear();
foreach (var obj in listData)
{
_listData.Add(obj);
}
List.selectionMode = selectionMode;
List.numItems = _listData.Count;
List.ScrollToView(0);
_columnsCount = 5;
}
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 GetListDefaultItemResource(List<object> listData)
{
var itemData = listData.Find(t => t != null);
if (itemData is ShopGearData)
{
return ShopGearItem.URL;
}
return List.defaultItem;
}
}
}