90 lines
2.3 KiB
C#
90 lines
2.3 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using TMPro;
|
|
using System.Text.RegularExpressions;
|
|
|
|
public class get_item_prefab : MonoBehaviour
|
|
{
|
|
private static readonly Regex RichTextTagRegex = new Regex("<.*?>", RegexOptions.Compiled);
|
|
|
|
[SerializeField] private bool testColorChange;
|
|
[SerializeField] private Color defaultItemNameColor = Color.white;
|
|
public Image item_btm;
|
|
public Image itemProfile;
|
|
public TextMeshProUGUI itemAmount;
|
|
public Text itemName;
|
|
|
|
private void Awake()
|
|
{
|
|
ApplyItemNameColor(defaultItemNameColor);
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
if (!testColorChange)
|
|
{
|
|
ApplyItemNameColor(defaultItemNameColor);
|
|
}
|
|
}
|
|
|
|
public void Bind(Sprite icon, string displayName, int amount, Color qualityColor, Sprite backgroundSprite)
|
|
{
|
|
if (item_btm != null)
|
|
{
|
|
item_btm.sprite = backgroundSprite;
|
|
item_btm.color = backgroundSprite != null ? Color.white : qualityColor;
|
|
}
|
|
|
|
if (itemProfile != null)
|
|
{
|
|
itemProfile.sprite = icon;
|
|
itemProfile.enabled = icon != null;
|
|
itemProfile.color = icon != null ? Color.white : new Color(1f, 1f, 1f, 0f);
|
|
}
|
|
|
|
if (itemName != null)
|
|
{
|
|
string resolvedName = displayName ?? string.Empty;
|
|
itemName.supportRichText = testColorChange;
|
|
if (!testColorChange)
|
|
{
|
|
resolvedName = StripRichTextTags(resolvedName);
|
|
}
|
|
|
|
itemName.text = resolvedName;
|
|
}
|
|
|
|
ApplyItemNameColor(testColorChange ? qualityColor : defaultItemNameColor);
|
|
|
|
if (itemAmount != null)
|
|
{
|
|
if (amount <= 1)
|
|
{
|
|
itemAmount.text = string.Empty;
|
|
}
|
|
else
|
|
{
|
|
itemAmount.text = amount.ToString();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void ApplyItemNameColor(Color color)
|
|
{
|
|
if (itemName != null)
|
|
{
|
|
itemName.color = color;
|
|
}
|
|
}
|
|
|
|
private static string StripRichTextTags(string value)
|
|
{
|
|
if (string.IsNullOrEmpty(value))
|
|
{
|
|
return string.Empty;
|
|
}
|
|
|
|
return RichTextTagRegex.Replace(value, string.Empty);
|
|
}
|
|
}
|