Files
test/Assets/PerfectWorld/Scripts/Managers/EC_SlotStackCountDisplay.cs
2026-05-21 17:35:15 +07:00

91 lines
2.8 KiB
C#

using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace BrewMonster.Scripts.Managers
{
/// <summary>Shared stack count label lookup for inventory / storage slot buttons.</summary>
public static class EC_SlotStackCountDisplay
{
public static int ResolveStackCount(EC_IvtrItem item)
{
if (item == null)
return 0;
int n = item.GetCount();
if (n < 2 && item.m_iCount >= 2)
n = item.m_iCount;
return n;
}
public static void UpdateButton(Button button, EC_IvtrItem item)
{
UpdateButton(button, ResolveStackCount(item));
}
public static void UpdateButton(Button button, int count)
{
if (button == null)
return;
TMPro.TextMeshProUGUI tmpText = null;
Text legacyText = null;
var textTransform = FindStackCountTextTransform(button.transform);
if (textTransform != null)
{
tmpText = textTransform.GetComponent<TMPro.TextMeshProUGUI>();
legacyText = textTransform.GetComponent<Text>();
}
if (tmpText == null && legacyText == null)
{
tmpText = button.GetComponentInChildren<TMPro.TextMeshProUGUI>();
if (tmpText == null)
legacyText = button.GetComponentInChildren<Text>();
}
if (count > 1)
{
string countText = count.ToString();
if (tmpText != null)
{
tmpText.text = countText;
tmpText.gameObject.SetActive(true);
}
else if (legacyText != null)
{
legacyText.text = countText;
legacyText.gameObject.SetActive(true);
}
}
else
{
if (tmpText != null)
{
tmpText.text = "";
tmpText.gameObject.SetActive(false);
}
else if (legacyText != null)
{
legacyText.text = "";
legacyText.gameObject.SetActive(false);
}
}
}
static Transform FindStackCountTextTransform(Transform root)
{
if (root == null)
return null;
string[] names = { "Txt_Count", "text_quality", "text_quatity", "text_quantity", "image_amount" };
for (int n = 0; n < names.Length; n++)
{
var t = root.Find(names[n]);
if (t != null)
return t;
}
return null;
}
}
}