_TheStrongestSnail/TheStrongestSnail/Assets/Scripts/HistoryPanel.cs

134 lines
4.3 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class HistoryPanel : BasePanel
{
public Button returnBtn;
public List<HistoryItem> KillTimesList;//100次击杀统计
public GameObject historyItemPre;//物品预制体
public Transform ItemPreGrid;//预制体生成后的父物体
public List<GameObject> ItemList;
public TextMeshProUGUI throwEggText;//投入蜗牛蛋的文本
public TextMeshProUGUI getEggText;//获得蜗牛蛋的文本
public GameObject JournalPanelPre;//用户参与面板预制体
public Transform JournParent;//用户参与面板父物体
public List<GameObject> journalPanels=new List<GameObject>();
public override void Start()
{
base.Start();
returnBtn.onClick.AddListener(OnClickReBtn);
}
public void OnClickReBtn()
{
HidePanel();
}
public void SetKilledText(gameEscapeRoomKillCountResponseVo gameKill)//近100次被杀记录的文本修改方法
{
for (int i=0;i<KillTimesList.Count;i++)
{
if (KillTimesList[i].roomNo == gameKill.roomNo)
{
KillTimesList[i].TextPro.text = gameKill.kill+"次";//给次数值
}
}
}
public void CreateJournalPanel(List<dataList> gameData) // 创建参与历史记录
{
foreach (dataList item in gameData)
{
// 实例化一个新的 JournalPanelPre 对象
GameObject newPanel = Instantiate(JournalPanelPre, JournParent);
// 获取新实例的 JournalPanel 组件
JournalPanel panelComponent = newPanel.GetComponent<JournalPanel>();
// 设置实例化对象的内容
panelComponent.gameNoText.text = item.gameNo + "期";
panelComponent.gameNo = item.gameNo;
panelComponent.betText.text = item.bet.ToString();
panelComponent.roomNo = item.roomNo;
panelComponent.roomNoKill = item.roomNoKill;
panelComponent.beansCoinText.text = item.beansCoin.ToString();
panelComponent.createTimeText.text = item.createTime;
panelComponent.outcome = item.outcome;
// 判断是否已存在
bool exists = false;
foreach (GameObject panel in journalPanels)
{
if (panel.GetComponent<JournalPanel>().gameNo == item.gameNo)
{
exists = true;
break;
}
}
// 如果不存在,则添加到列表
if (!exists)
{
journalPanels.Add(newPanel);
Debug.Log("加入一个面板到列表");
}
else
{
// 如果已存在,可以根据需求决定是否销毁该实例
Destroy(newPanel);
}
}
}
public void SetKillHouse(gameEscapeModel gameEscapeModelList) // 记录近 10 次被击杀的房间
{
string[] parts = gameEscapeModelList.roomNoKill.Split(","); // 根据 ',' 分割字符串
Debug.Log("被击杀的房间号: " + gameEscapeModelList.roomNoKill + "---------------");
foreach (string part in parts)
{
foreach (HistoryItem item in KillTimesList)
{
if (item.roomNo == int.Parse(part))
{
// 实例化新的对象
GameObject newHistoryItem = Instantiate(historyItemPre, ItemPreGrid);
// 设置实例的内容
HistoryItem historyItemComponent = newHistoryItem.GetComponent<HistoryItem>();
historyItemComponent.nameTextPro.text = item.nameTextPro.text; // 房间名字
historyItemComponent.TextPro.text = gameEscapeModelList.gameNo + "期"; // 期号
historyItemComponent.image.sprite = item.sprite; // 背景图片
// 限制列表最多保存 10 个实例
if (ItemList.Count >= 10)
{
Destroy(ItemList[0]); // 销毁最早的对象
ItemList.RemoveAt(0); // 从列表移除最早的对象
}
// 将新实例添加到列表
ItemList.Add(newHistoryItem);
}
}
}
}
}