TH1/Unity/Assets/Scripts/TH1_UI/LibraryUI/LibraryWonderUI.cs
2025-09-01 19:27:18 +08:00

225 lines
10 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using UnityEngine;
using Logic;
using RuntimeData;
using Animancer;
using UnityEngine.UI;
using System.Collections.Generic;
using System.Net;
using Logic.Multilingual;
using TH1Resource;
using TMPro;
using Unity.VisualScripting;
namespace UI.LibraryUI
{
public class LibraryWonderUI : LibrarySubUIBase
{
public WonderLibrary WonderSelected;
public LibraryWonderUI(LibrarySubUIType type,Button enterButton, GameObject panel,LibraryUI libraryUI) : base(type,enterButton,panel,libraryUI)
{
}
//更新listpanel的信息
protected override void UpdateListPanel()
{
var table = ListPanel.transform.Find("Scroll View/Viewport/Content");
//构造needList需要展示的giant list
int needCount = 7;
var needList = new List<WonderLibrary>();
needList.Add(WonderLibrary.EgyptianRemiliaPEACE);
needList.Add(WonderLibrary.EgyptianRemiliaKNOWLEDGE);
needList.Add(WonderLibrary.EgyptianRemiliaTRADE);
needList.Add(WonderLibrary.EgyptianRemiliaWEALTH);
needList.Add(WonderLibrary.EgyptianRemiliaPOWER);
needList.Add(WonderLibrary.EgyptianRemiliaPARK);
needList.Add(WonderLibrary.EgyptianRemiliaEYE);
//col=4 一行有4个item
int col = 4;
//根据需要展示的情况确定table下的显示位置够不够多的setactive false少的clone
int hasCount = table.childCount * col;
//如果坑位少于需要展示的数量,增加坑位
var sampleRow = table.Find("SampleRow");
while (hasCount < needCount)
{
GameObject.Instantiate(sampleRow, table);
hasCount += col;
}
//将最后一行多出来的坑位setactive false
int lastCount = needCount;
while (lastCount < hasCount)
{
table.GetChild(lastCount / col).GetChild(lastCount % col).gameObject.SetActive(false);
lastCount++;
}
//总计的成就星星数量
int allStar = 0;
//设置每一个wonder
for(int i = 0;i < needList.Count; i++)
{
var wonder = needList[i];
if(!Table.Instance.GridAndResourceDataAssets.GetWonderInfo( wonder, out var wonderInfo))continue;
//更换sprite
if (table.GetChild(i / col) == null) continue;
var slot = table.GetChild( i / col).GetChild(i % col);
if (slot == null) continue;
slot.Find("Size/TribeIconMask/TribeIcon").GetComponent<Image>().sprite = wonderInfo.Sprite;
Button slotButton = slot.GetComponent<Button>();
if (slotButton == null)
slotButton = slot.gameObject.AddComponent<Button>();
// 移除旧的监听器,防止重复添加
slotButton.onClick.RemoveAllListeners();
// 添加新的监听器,使用 lambda 表达式捕获当前的 giant 变量
slotButton.onClick.AddListener(() =>
{
// 当按钮被点击时,调用 UpdateGiantInfoData 并传入对应的 GiantType
WonderListItemOnClick(slot,wonder);
});
//处理成就星星的显示
uint bigid = 3;
uint smallid = wonder switch
{
WonderLibrary.EgyptianRemiliaPEACE => 1,
WonderLibrary.EgyptianRemiliaKNOWLEDGE => 2,
WonderLibrary.EgyptianRemiliaTRADE => 3,
WonderLibrary.EgyptianRemiliaWEALTH => 4,
WonderLibrary.EgyptianRemiliaPOWER => 5,
WonderLibrary.EgyptianRemiliaPARK => 6,
_ => 7
};
for (uint k = 1; k <= 3; k++)
{
//star坑位
var star = slot.Find($"Size/Stars/Star{k}")?.GetComponent<Image>();
bool finished = AchievementDataManager.Instance.IsFinished(bigid, smallid, k);
if (star != null)
star.sprite = finished ? ResourceCache.Instance.SpriteCache.AchievementStar : ResourceCache.Instance.SpriteCache.AchievementStarGray ;
if (finished) allStar++;
}
}
//设置总成就星星数量的显示
var textUI = ListPanel.transform.Find("TitleBar/StarCount/Text")?.GetComponent<TextMeshProUGUI>();
if (textUI != null)
textUI.text = allStar.ToString() + " / " + (needCount * 3).ToString();
}
private void WonderListItemOnClick(Transform selected, WonderLibrary wonder)
{
//消除之前选中对象的红圈,换成当前这个
SelectedListItem?.Find("Size/Selected")?.gameObject.SetActive(false);
selected.Find("Size/Selected")?.gameObject.SetActive(true);
SelectedListItem = selected;
WonderSelected = wonder;
InfoPanel.GetComponent<AnimancerComponent>()?.Play(ResourceCache.Instance.AnimCache.UICommonPanelFadeOut);
Timer.Instance.TimerRegister(this, () =>
{
UpdateInfoPanel();
InfoPanel.GetComponent<AnimancerComponent>()?.Play(ResourceCache.Instance.AnimCache.UICommonPanelFadeIn);
},ResourceCache.Instance.AnimCache.UICommonPanelFadeOut.length,"LibraryWonderUI_WonderListItem");
}
//更新infopanel的信息
protected override void UpdateInfoPanel()
{
var wonder = WonderSelected;
if (!Table.Instance.GridAndResourceDataAssets.GetWonderInfo(wonder, out var wonderInfo)) return;
if (!Table.Instance.LibraryDataAssets.GetLibraryInfoByWonder(wonder, out var libraryInfo)) return;
if (!Table.Instance.PlayerDataAssets.GetPlayerInfoByCivId(wonderInfo.CivId,wonderInfo.ForceId, out var playerInfo)) return;
var image = InfoPanel.transform.Find("WonderImage/Wonder");
var info = InfoPanel.transform.Find("WonderInfo");
//设置立绘
image.GetComponent<Image>().sprite = wonderInfo.Sprite;
//设置角色名称
MultilingualManager.Instance.SetUIText(InfoPanel.transform.Find("Title")?.GetComponent<TextMeshProUGUI>(),libraryInfo.Name);
//设置帝国名称
//设置角色介绍
MultilingualManager.Instance.SetUIText(info.Find("Info1/Desc")?.GetComponent<TextMeshProUGUI>(),libraryInfo.Desc);
//设置角色读白
//MultilingualManager.Instance.SetUIText(info.Find("Info1/Diag/Text")?.GetComponent<TextMeshProUGUI>(),libraryInfo.Diag);
//设置角色读白角色头像
//info.Find("Info1/Diag/AvatarCircle/Avatar").GetComponent<Image>().sprite = unitInfo.Sprite;
//设置角色读白对话框背景颜色
//if(info.Find("Info1/Diag") != null)
// info.Find("Info1/Diag").GetComponent<Image>().color = libraryInfo.DiagColor;
//处理成就
uint bigid = 3;
uint smallid = wonder switch
{
WonderLibrary.EgyptianRemiliaPEACE => 1,
WonderLibrary.EgyptianRemiliaKNOWLEDGE => 2,
WonderLibrary.EgyptianRemiliaTRADE => 3,
WonderLibrary.EgyptianRemiliaWEALTH => 4,
WonderLibrary.EgyptianRemiliaPOWER => 5,
WonderLibrary.EgyptianRemiliaPARK => 6,
_ => 7
};
for (uint i = 1; i <= 3; i++)
{
//star坑位
var star = info.Find($"Info3/Star{i}/Star")?.GetComponent<Image>();
//成就标题star坑位
var title = info.Find($"Info3/Star{i}/Title")?.GetComponent<TextMeshProUGUI>();
//成就描述star坑位
var desc = info.Find($"Info3/Star{i}/Desc")?.GetComponent<TextMeshProUGUI>();
var infoData = AchievementDataManager.Instance.Achievement.GetAchievementItem(bigid, smallid, i);
Debug.Log(bigid + " " + smallid + " " +i);
bool finished = AchievementDataManager.Instance.IsFinished(bigid, smallid, i);
if (infoData == null) continue;
MultilingualManager.Instance.SetUIText(title,infoData.Name);
MultilingualManager.Instance.SetUIText(desc,infoData.Desc);
if(title != null)
title.color = finished ? Table.Instance.ColorDataAssets.CommonUITextColor : Table.Instance.ColorDataAssets.CommonUITextColorGray ;
if(desc != null)
desc.color = finished ? Table.Instance.ColorDataAssets.CommonUITextColor : Table.Instance.ColorDataAssets.CommonUITextColorGray ;
if (star != null)
star.sprite = finished ? ResourceCache.Instance.SpriteCache.AchievementStar : ResourceCache.Instance.SpriteCache.AchievementStarGray ;
}
}
//SwitchTo
public override void Show()
{
UpdateListPanel();
SetDefaultItem();
UpdateInfoPanel();
OpenPanel();
}
public override void Hide()
{
ClosePanel();
}
//每个subpanel都有一个初始化函数如果切换到这个页面要进行一次初始化选择默认选择左上角的单位)
protected override void SetDefaultItem()
{
if (ListTable == null) return;
DefaultListItem = ListTable.transform.GetChild(0).GetChild(0);
SelectedListItem?.Find("Size/Selected")?.gameObject.SetActive(false);
DefaultListItem?.Find("Size/Selected")?.gameObject.SetActive(true);
SelectedListItem = DefaultListItem;
WonderSelected = WonderLibrary.EgyptianRemiliaPEACE;
}
}
}