157 lines
3.7 KiB
C#
157 lines
3.7 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using LitJson;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class MailPanel : BasePanel
|
|
{
|
|
//关闭
|
|
public Button btnClose;
|
|
//一键删除
|
|
public Button btnOneKeyDel;
|
|
//一键领取
|
|
public Button btnOneKeyGet;
|
|
|
|
public ScrollRect sv;
|
|
|
|
public GameObject obj;
|
|
|
|
private string filePath;
|
|
|
|
public List<MailInfo> info;
|
|
|
|
private List<GameObject> mailObjects;
|
|
|
|
void Awake()
|
|
{
|
|
filePath = Path.Combine(Application.persistentDataPath, GameDataMgr.Instance.player.openId);
|
|
mailObjects=new List<GameObject>();
|
|
}
|
|
|
|
public override void Init()
|
|
{
|
|
Debug.Log(Application.persistentDataPath);
|
|
LoadMailData();
|
|
UpdatePanel();
|
|
btnClose.onClick.AddListener(() =>
|
|
{
|
|
UIManager.Instance.HidePanel<MailPanel>(false);
|
|
});
|
|
|
|
btnOneKeyGet.onClick.AddListener(()=>
|
|
{
|
|
if (info!=null&&info.Count>0)
|
|
{
|
|
GetAllMail();
|
|
}
|
|
});
|
|
|
|
btnOneKeyDel.onClick.AddListener(() =>
|
|
{
|
|
OneKeyDelMail();
|
|
});
|
|
}
|
|
|
|
//更新面板
|
|
public void UpdatePanel()
|
|
{
|
|
if (info.Count>0)
|
|
{
|
|
GameObject go;
|
|
for (int i = 0; i < info.Count; i++)
|
|
{
|
|
go = Instantiate(obj);
|
|
go.transform.SetParent(sv.content, false);
|
|
go.GetComponent<MailObject>().UpdatePanel(info[i]);
|
|
mailObjects.Add(go);
|
|
}
|
|
}
|
|
}
|
|
|
|
// 加载本地邮件数据
|
|
private void LoadMailData()
|
|
{
|
|
if (File.Exists(filePath))
|
|
{
|
|
string json = File.ReadAllText(filePath);
|
|
info = JsonMgr.Instance.LoadData<List<MailInfo>>(json);
|
|
}
|
|
else
|
|
{
|
|
info = new List<MailInfo>();
|
|
}
|
|
}
|
|
|
|
// 检查是否可以领取邮件
|
|
public bool CanClaimMail(MailInfo mail)
|
|
{
|
|
DateTime now = DateTime.Now;
|
|
DateTime sendEnd;
|
|
if (DateTime.TryParse(mail.mailData.send_end, out sendEnd))
|
|
{
|
|
return now <= sendEnd && !mail.isOpen;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// 一键删除已经领取的邮件
|
|
public void OneKeyDelMail()
|
|
{
|
|
for (int i = 0; i < info.Count; i++)
|
|
{
|
|
if (info[i] != null && info[i].isOpen)
|
|
{
|
|
info.Remove(info[i]);
|
|
}
|
|
|
|
if (mailObjects[i].GetComponent<MailObject>().imgGet.activeSelf)
|
|
{
|
|
Destroy(mailObjects[i]);
|
|
mailObjects.Remove(mailObjects[i]);
|
|
}
|
|
}
|
|
SaveMailData();
|
|
}
|
|
|
|
// 领取全部邮件
|
|
public void GetAllMail()
|
|
{
|
|
for (int i = 0; i < info.Count; i++)
|
|
{
|
|
if (info[i] != null && CanClaimMail(info[i]))
|
|
{
|
|
info[i].isOpen = true; // 更新标记为已领取
|
|
SaveMailData(); // 保存状态
|
|
GameDataMgr.Instance.player.gold += info[i].mailData.gold;
|
|
}
|
|
}
|
|
StartCoroutine(NetMgr.Instance.ChangeDataPost(GameDataMgr.Instance.player));
|
|
}
|
|
|
|
// 领取单个邮件
|
|
public void ClaimMail(int mailId)
|
|
{
|
|
MailInfo mail = info.Find(m => m.mailData.id == mailId);
|
|
if (mail != null && CanClaimMail(mail))
|
|
{
|
|
mail.isOpen = true; // 更新标记为已领取
|
|
SaveMailData(); // 保存状态
|
|
Debug.Log("领取成功");
|
|
}
|
|
else
|
|
{
|
|
Debug.Log("无法领取,邮件已过期或已领取");
|
|
}
|
|
}
|
|
|
|
// 保存邮件数据到本地
|
|
public void SaveMailData()
|
|
{
|
|
string json = JsonMapper.ToJson(info);
|
|
File.WriteAllText(filePath, json);
|
|
}
|
|
}
|