423 lines
17 KiB
C#
423 lines
17 KiB
C#
using Assets.Framework.Tool;
|
|
using Assets.MyGame.Scripts.Net;
|
|
using Assets.MyGame.Scripts.Net.APIRequest.Params;
|
|
using Assets.MyGame.Scripts.Net.APIResponses.HomeMain_User;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.AddressableAssets;
|
|
using UnityEngine.Rendering;
|
|
using UnityEngine.ResourceManagement.AsyncOperations;
|
|
using UnityEngine.UI;
|
|
|
|
public class UIHome_BY_EmailPanel : SubUIBase
|
|
{
|
|
public GameObject obj_ShowEmail;
|
|
public TextMeshProUGUI tMPText_Header;
|
|
public TextMeshProUGUI tMPText_Content;
|
|
public TextMeshProUGUI tMPText_EmailCount;
|
|
public Button btn_Receive;
|
|
public Button btn_Dlete;
|
|
public Button btn_AllDlete;
|
|
public Button btn_AllReceive;
|
|
|
|
public EmalItem itemPrefab;
|
|
public Transform itemPrefabParent;
|
|
public GameObject propPrefab;
|
|
public Transform propPrefabParent;
|
|
public string currEmailID;
|
|
|
|
public Dictionary<string,GetEmailByPageResponse> dic_GetEmailByPageResponses = new Dictionary<string, GetEmailByPageResponse>();
|
|
public Dictionary<string,EmalItem> dic_EmalItems = new Dictionary<string, EmalItem>();
|
|
|
|
public override void Awake()
|
|
{
|
|
base.Awake();
|
|
GameManager.Instance.OnUserDataChanged += RefreshUI;
|
|
|
|
btn_Receive.onClick.AddListener(() =>
|
|
{
|
|
APIRequestList.RewardEmail(new RewardEmailParam
|
|
{
|
|
id=currEmailID,
|
|
type=1
|
|
}, (str) =>
|
|
{
|
|
UIManager.Instance.ShowBoardError("领取成功");
|
|
// 弹出获得面板
|
|
if (dic_GetEmailByPageResponses.TryGetValue(currEmailID, out var email))
|
|
{
|
|
var dataList = BuildRewardDataList(email);
|
|
if (dataList.Count > 0)
|
|
UIManager.Instance.ShowHuoDe(dataList);
|
|
}
|
|
RewardEmail(currEmailID);
|
|
// 领取后刷新用户数据
|
|
var userId = GameManager.Instance?.user?.id;
|
|
if (!string.IsNullOrEmpty(userId))
|
|
APIRequestList.GetUser(userId, a => GameManager.Instance.UpdataUserData(a));
|
|
});
|
|
});
|
|
btn_AllReceive.onClick.AddListener(() =>
|
|
{
|
|
APIRequestList.RewardEmail(new RewardEmailParam
|
|
{
|
|
id = "",
|
|
type = 2
|
|
}, (str) =>
|
|
{
|
|
UIManager.Instance.ShowBoardError("领取成功");
|
|
// 合并所有可领取邮件的奖励并弹出获得面板
|
|
var dataList = new List<BackPackPropItemData>();
|
|
foreach (var kvp in dic_GetEmailByPageResponses)
|
|
{
|
|
if (kvp.Value.isReward) continue; // 已领取的跳过
|
|
dataList.AddRange(BuildRewardDataList(kvp.Value));
|
|
}
|
|
if (dataList.Count > 0)
|
|
UIManager.Instance.ShowHuoDe(dataList);
|
|
foreach (var item in dic_EmalItems.Keys)
|
|
{
|
|
RewardEmail(item);
|
|
}
|
|
//for (int i = 0; i < propPrefabParent.childCount; i++)
|
|
//{
|
|
// propPrefabParent.GetChild(i).GetChild(1).gameObject.SetActive(true);
|
|
//}
|
|
});
|
|
|
|
});
|
|
btn_Dlete.onClick.AddListener(() =>
|
|
{
|
|
APIRequestList.DeleteEmail(new DeleteEmailParam
|
|
{
|
|
id = currEmailID,
|
|
type = 1
|
|
}, (str) =>
|
|
{
|
|
UIManager.Instance.ShowBoardError("删除成功");
|
|
dic_GetEmailByPageResponses.Remove(currEmailID);
|
|
; Destroy(dic_EmalItems[currEmailID].gameObject);
|
|
dic_EmalItems.Remove(currEmailID);
|
|
if (dic_EmalItems.Count == 0)
|
|
{
|
|
obj_ShowEmail.SetActive(false);
|
|
}
|
|
else
|
|
{
|
|
obj_ShowEmail.SetActive(true);
|
|
}
|
|
tMPText_EmailCount.text = itemPrefabParent.childCount.ToString();
|
|
|
|
});
|
|
|
|
});
|
|
btn_AllDlete.onClick.AddListener(() =>
|
|
{
|
|
APIRequestList.DeleteEmail(new DeleteEmailParam
|
|
{
|
|
id = "",
|
|
type = 2
|
|
}, (str) =>
|
|
{
|
|
|
|
UIManager.Instance.ShowBoardError("删除成功");
|
|
|
|
|
|
|
|
|
|
foreach (var item in dic_EmalItems.Keys.ToList())
|
|
{
|
|
if (dic_GetEmailByPageResponses[item].rewardItems !=null&& dic_GetEmailByPageResponses[item].rewardItems.Count > 0|| dic_GetEmailByPageResponses[item].rewardGold>0|| dic_GetEmailByPageResponses[item].rewardDiamond>0)
|
|
{
|
|
if (dic_GetEmailByPageResponses[item].isReward == true)
|
|
{
|
|
Destroy(dic_EmalItems[item].gameObject);
|
|
|
|
dic_GetEmailByPageResponses.Remove(item);
|
|
|
|
dic_EmalItems.Remove(item);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (dic_GetEmailByPageResponses[item].isRead == true)
|
|
{
|
|
Destroy(dic_EmalItems[item].gameObject);
|
|
dic_GetEmailByPageResponses.Remove(item);
|
|
dic_EmalItems.Remove(item);
|
|
}
|
|
}
|
|
}
|
|
tMPText_EmailCount.text = itemPrefabParent.childCount.ToString();
|
|
if (dic_EmalItems.Count == 0)
|
|
{
|
|
obj_ShowEmail.SetActive(false);
|
|
}
|
|
else
|
|
{
|
|
obj_ShowEmail.SetActive(true);
|
|
}
|
|
});
|
|
});
|
|
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
// 防止面板被销毁后仍留在事件里(否则 UpdataUserData 会调到已销毁的面板并中断事件链)
|
|
if (GameManager.Instance != null)
|
|
GameManager.Instance.OnUserDataChanged -= RefreshUI;
|
|
}
|
|
|
|
private void RewardEmail(string key)
|
|
{
|
|
|
|
dic_GetEmailByPageResponses[key].isReward = true;
|
|
dic_EmalItems[key].obj_LingQu.SetActive(true);
|
|
btn_Receive.gameObject.SetActive(false);
|
|
btn_Dlete.gameObject.SetActive(true);
|
|
for (int i = 0; i < propPrefabParent.childCount; i++)
|
|
{
|
|
propPrefabParent.GetChild(i).GetChild(1).gameObject.SetActive(true);
|
|
}
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 从单封邮件的奖励数据构建 BackPackPropItemData 列表(金币/钻石/道具)。
|
|
/// 数量为 0 的不加入列表。
|
|
/// </summary>
|
|
private List<BackPackPropItemData> BuildRewardDataList(GetEmailByPageResponse email)
|
|
{
|
|
var list = new List<BackPackPropItemData>();
|
|
if (email == null) return list;
|
|
|
|
if (email.rewardGold > 0)
|
|
{
|
|
list.Add(new BackPackPropItemData { giftType = 1, count = (int)email.rewardGold });
|
|
}
|
|
if (email.rewardDiamond > 0)
|
|
{
|
|
list.Add(new BackPackPropItemData { giftType = 2, count = (int)email.rewardDiamond });
|
|
}
|
|
if (email.rewardItems != null)
|
|
{
|
|
foreach (var prop in email.rewardItems)
|
|
{
|
|
if (prop == null) continue;
|
|
string url = ResolveItemImageUrl(prop);
|
|
list.Add(new BackPackPropItemData
|
|
{
|
|
giftType = 3,
|
|
url = string.IsNullOrEmpty(url) ? "" : APIManager.baseURL + url,
|
|
count = prop.count,
|
|
propEnum = (PropEnum)ResolveItemPropType(prop),
|
|
});
|
|
}
|
|
}
|
|
return list;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 解析邮件道具图片相对地址:优先用后端下发的 imageUrl,
|
|
/// 未下发时按约定格式补全 /uploads/{编号}.png。
|
|
/// </summary>
|
|
private static string ResolveItemImageUrl(RewardItem prop)
|
|
{
|
|
if (prop == null) return "";
|
|
if (!string.IsNullOrEmpty(prop.imageUrl)) return prop.imageUrl;
|
|
return string.IsNullOrEmpty(prop.number) ? "" : $"/uploads/{prop.number}.png";
|
|
}
|
|
|
|
/// <summary>通过 number 在 propConfig 中查找道具类型(用于背景框)</summary>
|
|
private static int ResolveItemPropType(RewardItem prop)
|
|
{
|
|
if (prop == null || string.IsNullOrEmpty(prop.number)) return prop?.type ?? 0;
|
|
var match = GameManager.Instance?.gameConfig?.propConfig?.Find(p => p.number == prop.number);
|
|
return match?.type ?? prop.type;
|
|
}
|
|
|
|
protected override void OnShow(object args = null)
|
|
{
|
|
base.OnShow(args);
|
|
|
|
foreach (var item in dic_EmalItems.Values)
|
|
{
|
|
Destroy(item.gameObject);
|
|
}
|
|
dic_GetEmailByPageResponses.Clear();
|
|
dic_EmalItems.Clear();
|
|
if (args != null)
|
|
{
|
|
List<GetEmailByPageResponse> getEmailByPageResponse = (List<GetEmailByPageResponse>)args;
|
|
if (getEmailByPageResponse.Count==0)
|
|
{
|
|
obj_ShowEmail.SetActive(false);
|
|
}
|
|
else
|
|
{
|
|
obj_ShowEmail.SetActive(true);
|
|
}
|
|
foreach (var item in getEmailByPageResponse)
|
|
{
|
|
dic_GetEmailByPageResponses[item.id] = item;
|
|
|
|
var emailItem = Instantiate(itemPrefab.gameObject, itemPrefabParent).GetComponent<EmalItem>();
|
|
emailItem.Init(this, item.title, item.sendTime, item.isReward,item.isRead);
|
|
dic_EmalItems[item.id] = emailItem;
|
|
emailItem.GetComponent<Toggle>().onValueChanged.AddListener((a) =>
|
|
{
|
|
if (a)
|
|
{
|
|
currEmailID = item.id;
|
|
tMPText_Header.text = item.title;
|
|
tMPText_Content.text = item.content;
|
|
APIRequestList.GetEmail(new GetEmailParam
|
|
{
|
|
id = currEmailID,
|
|
}, (GetEmailByPageResponse) =>
|
|
{
|
|
dic_EmalItems[ GetEmailByPageResponse.id].obj_YiDu.SetActive(false);
|
|
dic_GetEmailByPageResponses[ GetEmailByPageResponse.id].isRead=(true);
|
|
});
|
|
for (int i = propPrefabParent.childCount; i > 0; i--)
|
|
{
|
|
Destroy(propPrefabParent.GetChild(i-1).gameObject);
|
|
}
|
|
if (item.rewardGold>0)
|
|
{
|
|
GameObject obj = Instantiate(propPrefab, propPrefabParent);
|
|
ResourceRequest request = Resources.LoadAsync<Sprite>("gold");
|
|
request.completed += (op) =>
|
|
{
|
|
if (op is ResourceRequest req && req.asset is Sprite sprite)
|
|
{
|
|
obj.transform.GetChild(0).GetComponent<Image>().sprite = sprite;
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("加载失败: ");
|
|
}
|
|
};
|
|
//Addressables.LoadAssetAsync<Sprite>("Assets/UIResources/Home/邮件/图层 2311.png").Completed+=(AsyncOperationHandle<Sprite> op) =>
|
|
//{
|
|
// if (op.Status==AsyncOperationStatus.Succeeded)
|
|
// {
|
|
// obj.transform.GetChild(0).GetComponent<Image>().sprite = op.Result;
|
|
// }
|
|
// else
|
|
// {
|
|
// Debug.LogError("加载失败: " + op.OperationException);
|
|
// }
|
|
|
|
//};
|
|
obj.transform.GetChild(0).GetChild(0).GetComponent<TextMeshProUGUI>().text = item.rewardGold.ToString();
|
|
if (item.isReward)
|
|
{
|
|
obj.transform.GetChild(1).gameObject.SetActive(true);
|
|
btn_Dlete.gameObject.SetActive(true);
|
|
btn_Receive.gameObject.SetActive(false);
|
|
}
|
|
else
|
|
{
|
|
btn_Dlete.gameObject.SetActive(false);
|
|
btn_Receive.gameObject.SetActive(true);
|
|
|
|
}
|
|
}
|
|
if (item.rewardDiamond>0)
|
|
{
|
|
|
|
GameObject obj = Instantiate(propPrefab, propPrefabParent);
|
|
ResourceRequest request = Resources.LoadAsync<Sprite>("diamond");
|
|
request.completed += (op) =>
|
|
{
|
|
if (op is ResourceRequest req && req.asset is Sprite sprite)
|
|
{
|
|
obj.transform.GetChild(0).GetComponent<Image>().sprite = sprite;
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("加载失败: ");
|
|
}
|
|
};
|
|
//Addressables.LoadAssetAsync<Sprite>("Assets/UIResources/Home/邮件/图层 2314.png").Completed += (AsyncOperationHandle<Sprite> op) =>
|
|
//{
|
|
// if (op.Status == AsyncOperationStatus.Succeeded)
|
|
// {
|
|
// obj.transform.GetChild(0).GetComponent<Image>().sprite = op.Result;
|
|
// }
|
|
// else
|
|
// {
|
|
// Debug.LogError("加载失败: " + op.OperationException);
|
|
// }
|
|
|
|
//};
|
|
obj.transform.GetChild(0).GetChild(0).GetComponent<TextMeshProUGUI>().text = item.rewardDiamond.ToString();
|
|
if (item.isReward)
|
|
{
|
|
obj.transform.GetChild(1).gameObject.SetActive(true);
|
|
btn_Dlete.gameObject.SetActive(true);
|
|
btn_Receive.gameObject.SetActive(false);
|
|
}
|
|
else
|
|
{
|
|
btn_Dlete.gameObject.SetActive(false);
|
|
btn_Receive.gameObject.SetActive(true);
|
|
|
|
}
|
|
}
|
|
if (item.rewardItems.Count>0)
|
|
{
|
|
foreach (var prop in item.rewardItems)
|
|
{
|
|
GameObject obj = Instantiate(propPrefab, propPrefabParent);
|
|
string itemUrl = ResolveItemImageUrl(prop);
|
|
if (!string.IsNullOrEmpty(itemUrl))
|
|
{
|
|
WebTool.DownImage(APIManager.baseURL + itemUrl, (texture) =>
|
|
{
|
|
if (obj == null) return;
|
|
obj.transform.GetChild(0).GetComponent<Image>().sprite = Sprite.Create(texture,new Rect(0f,0f,texture.width,texture.height),Vector2.one*0.5f);
|
|
});
|
|
}
|
|
obj.transform.GetChild(0).GetChild(0).GetComponent<TextMeshProUGUI>().text = prop.count.ToString();
|
|
if (item.isReward)
|
|
{
|
|
obj.transform.GetChild(1).gameObject.SetActive(true);
|
|
btn_Dlete.gameObject.SetActive(true);
|
|
btn_Receive.gameObject.SetActive(false);
|
|
}
|
|
else
|
|
{
|
|
btn_Dlete.gameObject.SetActive(false);
|
|
btn_Receive.gameObject.SetActive(true);
|
|
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
});
|
|
}
|
|
|
|
tMPText_EmailCount.text=itemPrefabParent.childCount.ToString();
|
|
}
|
|
else
|
|
{
|
|
obj_ShowEmail.SetActive(false);
|
|
}
|
|
|
|
}
|
|
|
|
private void RefreshUI()
|
|
{
|
|
// 邮件面板没有直接的金币/钻石文本,刷新由领取后 GetUser + UpdataUserData 触发
|
|
// 其他订阅 OnUserDataChanged 的面板(主界面、商店、背包等)会自动更新
|
|
}
|
|
}
|