112 lines
4 KiB
C#
112 lines
4 KiB
C#
|
|
using UnityEngine;
|
|||
|
|
using UnityEngine.UI;
|
|||
|
|
using TMPro;
|
|||
|
|
using System.Text;
|
|||
|
|
|
|||
|
|
public class UI_MailItem : MonoBehaviour
|
|||
|
|
{
|
|||
|
|
[Header("UI Components")]
|
|||
|
|
[SerializeField] private TMP_Text titleText;
|
|||
|
|
[SerializeField] private TMP_Text senderText;
|
|||
|
|
[SerializeField] private Image iconImage;
|
|||
|
|
[SerializeField] private Button interactionButton;
|
|||
|
|
[SerializeField] private CanvasGroup canvasGroup;
|
|||
|
|
[SerializeField] private GameObject newMailIndicator;
|
|||
|
|
[SerializeField] private ShopVisualConfig visualConfig;
|
|||
|
|
|
|||
|
|
[Header("Settings")]
|
|||
|
|
[SerializeField] private MailIconDatabase iconDb;
|
|||
|
|
|
|||
|
|
private MailEntry _mailData;
|
|||
|
|
|
|||
|
|
private void Awake()
|
|||
|
|
{
|
|||
|
|
if (interactionButton == null)
|
|||
|
|
interactionButton = GetComponent<Button>();
|
|||
|
|
|
|||
|
|
if (canvasGroup == null)
|
|||
|
|
canvasGroup = GetComponent<CanvasGroup>();
|
|||
|
|
|
|||
|
|
if (interactionButton != null)
|
|||
|
|
interactionButton.onClick.AddListener(OnItemClicked);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void OnItemClicked()
|
|||
|
|
{
|
|||
|
|
if (_mailData != null && MANAGER_WindowsBase.Instance != null && MANAGER_WindowsBase.Instance.UI != null)
|
|||
|
|
{
|
|||
|
|
MANAGER_WindowsBase.Instance.UI.ShowMailDetails(_mailData);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void Setup(MailEntry data)
|
|||
|
|
{
|
|||
|
|
// 1. Сохраняем ссылку на данные
|
|||
|
|
_mailData = data;
|
|||
|
|
|
|||
|
|
// 2. ПРИСВАИВАЕМ ТЕКСТ (Данные теперь берутся из visuals и content)
|
|||
|
|
// В MailContent обычно лежит заголовок (subject/title),
|
|||
|
|
// а в MailVisuals — имя отправителя.
|
|||
|
|
|
|||
|
|
if (titleText != null && data.content != null)
|
|||
|
|
{
|
|||
|
|
// Если в MailContent поле называется 'title' или 'subject'
|
|||
|
|
// Замените .title на нужное имя из вашего класса MailContent
|
|||
|
|
titleText.text = FormatCustomTags(data.content.title);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (senderText != null && data.visuals != null)
|
|||
|
|
{
|
|||
|
|
senderText.text = data.visuals.sender_name;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 3. ИКОНКА
|
|||
|
|
if (iconImage != null && data.visuals != null && iconDb != null)
|
|||
|
|
{
|
|||
|
|
Sprite foundIcon = iconDb.GetIcon(data.visuals.icon_id);
|
|||
|
|
|
|||
|
|
if (foundIcon != null)
|
|||
|
|
{
|
|||
|
|
iconImage.sprite = foundIcon;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
// Если иконка не найдена, удаляем объект, на котором висит Image
|
|||
|
|
Destroy(iconImage.gameObject);
|
|||
|
|
|
|||
|
|
// Обнуляем ссылку, чтобы другие части кода не пытались к ней обратиться
|
|||
|
|
iconImage = null;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 4. СОСТОЯНИЯ (Через Менеджер)
|
|||
|
|
bool claimed = MANAGER_Mail.Instance.IsRewardClaimed(data.id);
|
|||
|
|
bool read = MANAGER_Mail.Instance.IsMailRead(data.id);
|
|||
|
|
|
|||
|
|
// 5. ВИЗУАЛ
|
|||
|
|
if (interactionButton != null)
|
|||
|
|
interactionButton.interactable = !claimed;
|
|||
|
|
|
|||
|
|
if (canvasGroup != null)
|
|||
|
|
canvasGroup.alpha = claimed ? 0.4f : 1.0f;
|
|||
|
|
|
|||
|
|
if (newMailIndicator != null)
|
|||
|
|
newMailIndicator.SetActive(!read && !claimed);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private string FormatCustomTags(string rawText)
|
|||
|
|
{
|
|||
|
|
if (string.IsNullOrEmpty(rawText) || visualConfig == null) return rawText;
|
|||
|
|
|
|||
|
|
StringBuilder sb = new StringBuilder(rawText);
|
|||
|
|
|
|||
|
|
// Используем цвета из конфига, переводя их в Hex
|
|||
|
|
sb.Replace("[y]", $"<color={visualConfig.GetHex(visualConfig.yellowTag)}>");
|
|||
|
|
sb.Replace("[b]", $"<color={visualConfig.GetHex(visualConfig.blueTag)}>");
|
|||
|
|
sb.Replace("[r]", $"<color={visualConfig.GetHex(visualConfig.redTag)}>");
|
|||
|
|
sb.Replace("[o]", $"<color={visualConfig.GetHex(visualConfig.orangeTag)}>");
|
|||
|
|
sb.Replace("[/c]", "</color>");
|
|||
|
|
|
|||
|
|
return sb.ToString();
|
|||
|
|
}
|
|||
|
|
}
|