Files
zeling_v2/Assets/_Game/Scripts/UI/Inventory/ItemSlotView.cs
2026-06-05 18:41:33 +08:00

78 lines
2.7 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 UnityEngine;
using UnityEngine.UI;
using TMPro;
using BaseGames.Inventory;
using BaseGames.Localization;
namespace BaseGames.UI.Inventory
{
/// <summary>
/// 背包道具格子视图(对照 <see cref="BaseGames.UI.CharmCardView"/> 的显式序列化绑定风格)。
/// 由 <see cref="ItemInventoryPanel"/> 通过对象池实例化并调用 <see cref="Bind"/>。
/// </summary>
[DisallowMultipleComponent]
public class ItemSlotView : MonoBehaviour
{
[Header("Visual")]
[SerializeField] private Image _icon;
[SerializeField] private TMP_Text _countText;
[Tooltip("\"新获得\"角标节点IsNew 时显示)。")]
[SerializeField] private GameObject _newBadge;
[Header("Interaction")]
[SerializeField] private Button _selectButton;
[Tooltip("选中高亮节点(可空)。")]
[SerializeField] private GameObject _selectedHighlight;
private ItemSO _item;
public ItemSO Item => _item;
public Button SelectButton => _selectButton;
/// <summary>绑定道具条目。点击时回调 onSelect用于右侧详情面板。</summary>
public void Bind(InventoryEntry entry, Action<ItemSO> onSelect)
{
_item = entry.Item;
if (_item == null) return;
if (_icon != null)
{
_icon.sprite = _item.icon;
_icon.enabled = _item.icon != null;
}
if (_countText != null)
{
// 仅可叠加且数量 > 1 时显示数量角标
bool show = _item.stackable && entry.Count > 1;
_countText.text = show ? entry.Count.ToString() : string.Empty;
_countText.enabled = show;
}
if (_newBadge != null) _newBadge.SetActive(entry.IsNew);
if (_selectButton != null)
{
_selectButton.onClick.RemoveAllListeners();
var captured = _item;
if (onSelect != null)
_selectButton.onClick.AddListener(() => onSelect(captured));
}
}
public void SetSelected(bool selected)
{
if (_selectedHighlight != null) _selectedHighlight.SetActive(selected);
}
/// <summary>本地化道具名(容错:未找到键回落 itemId。</summary>
public string ResolveName()
{
if (_item == null) return string.Empty;
string loc = LocalizationManager.Get(_item.displayNameKey, LocalizationTable.Items);
return !string.IsNullOrEmpty(loc) && loc != _item.displayNameKey ? loc : _item.itemId;
}
}
}