using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; using GameServer.Client; using Bansonic; using UnityEngine; using UnityEngine.Networking; using UnityEngine.UI; public class UI_Panel_Mail : MonoBehaviour { public static event Action OnUnreadStateChanged; private static readonly Dictionary ServerMailImageCache = new Dictionary(StringComparer.Ordinal); private static readonly HashSet ServerMailImageLoadsInFlight = new HashSet(StringComparer.Ordinal); //1234567890 [SerializeField] float anim_Time = 0.5f; float Anim_Speed => 1f / anim_Time; [SerializeField] List ui_Anim; [Header("paths")] [SerializeField] private string editor_mailSO_Path = "Assets/Resources/so/mail_so"; [SerializeField] private string runtime_mailSO_Path = "so/mail_so"; [Header("source")] [SerializeField] private bool fetchMailsFromServer = false; [Header("mail objects")] public GameObject noticeSlotPrefab; [SerializeField] Transform content_Notice_Slot; [Header("rewards objects")] public GameObject rewardSlotPrefab; [SerializeField] Transform content_Reward_Slot; [Header("texts")] public Text thisMail_title; public Text thisMail_body; public Text thisMail_time; public Text thisMail_sender; List cachedMails = new List(); readonly Dictionary _serverMailImageUrls = new Dictionary(); mailSlotPrefab selectedSlot; mail_so selectedMailData; bool _mailServiceSubscribed; public bool UsesServerMail => fetchMailsFromServer; [Header("buttons")] public Button receive_this_mail; private void Update() { if (Input.GetKeyDown(KeyCode.Escape)) { gameObject.SetActive(false); } } void Start() { foreach (var item in ui_Anim) { item.speed = Anim_Speed; } BindTopLevelButtons(); InitializeMailSource(); } private void OnEnable() { InitializeMailSource(); } private void OnDisable() { UnsubscribeMailService(); } void InitializeMailSource() { if (!isActiveAndEnabled) { return; } BindTopLevelButtons(); if (!ShouldUseServerMail()) { UnsubscribeMailService(); BuildMailSlots(); return; } MailHttpService service = MailHttpService.Instance; if (service == null) { BuildMailSlots(new List()); return; } if (!_mailServiceSubscribed) { service.OnMailListChanged += HandleServerMailListChanged; _mailServiceSubscribed = true; } service.SetServerModeEnabled(true); HandleServerMailListChanged(service.CachedMails); service.RequestRefresh(); } void BuildMailSlots() { BuildMailSlots(LoadMailAssets()); } void BuildMailSlots(List mails) { if (noticeSlotPrefab == null || content_Notice_Slot == null) return; CleanupGeneratedRuntimeMails(); selectedSlot = null; selectedMailData = null; for (int i = content_Notice_Slot.childCount - 1; i >= 0; i--) { Destroy(content_Notice_Slot.GetChild(i).gameObject); } cachedMails = mails ?? new List(); if (cachedMails.Count == 0) { ShowMailDetails(null); ShowMailRewards(null); RefreshReceiveButton(); return; } ApplySavedState(cachedMails); for (int i = 0; i < cachedMails.Count; i++) { mail_so mail = cachedMails[i]; if (mail == null || mail.rewardList == null) { continue; } for (int rewardIndex = 0; rewardIndex < mail.rewardList.Count; rewardIndex++) { MailRewardGrantService.PopulateRewardDisplay(mail.rewardList[rewardIndex]); } } for (int i = 0; i < mails.Count; i++) { var mail = mails[i]; if (mail == null) continue; var go = Instantiate(noticeSlotPrefab, content_Notice_Slot); var slot = go.GetComponent(); if (selectedSlot == null) selectedSlot = slot; if (slot != null) { if (slot.mailTitle != null) slot.mailTitle.text = mail.mail_title; if (slot.mailSender != null) slot.mailSender.text = mail.mail_sender; if (slot.mailTime != null) slot.mailTime.text = mail.mail_date; if (slot.mailImage != null) slot.mailImage.sprite = mail.mail_image; TryBindServerMailImage(mail, slot); UpdateSlotVisuals(slot, mail); SetSelectedState(slot, false); var openButton = slot.openButton != null ? slot.openButton : go.GetComponent