198 lines
6.2 KiB
C#
198 lines
6.2 KiB
C#
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
using Logic;
|
||
using RuntimeData;
|
||
using TMPro;
|
||
public class DebugUI
|
||
{
|
||
private Main _main;
|
||
private MapData _mapData;
|
||
public GameObject RODebugUI;
|
||
|
||
private GameObject buttonGroup;
|
||
private GameObject panelGroup;
|
||
|
||
private Dictionary<string, GameObject> panelDict;
|
||
|
||
public bool NeedShow = false;
|
||
|
||
private uint _turn = 999;
|
||
public DebugUI(Main main, MapData mapData)
|
||
{
|
||
_main = main;
|
||
_mapData = mapData;
|
||
|
||
|
||
Transform root = UIManager.Instance.ROUIManager.transform;
|
||
Transform panelRoot = root.Find("DebugPanel");
|
||
if (panelRoot == null) return;
|
||
|
||
if (DebugCenter.Instance.DebugMode)
|
||
{
|
||
panelRoot.gameObject.SetActive(true);
|
||
}
|
||
else return;
|
||
|
||
RODebugUI = panelRoot.gameObject;
|
||
|
||
Button startButton = panelRoot.Find("ButtonStart")?.GetComponent<Button>();
|
||
if (startButton != null)
|
||
{
|
||
startButton.onClick.AddListener(() => ToggleMainPanel());
|
||
}
|
||
|
||
buttonGroup = panelRoot.Find("ButtonGroup")?.gameObject;
|
||
panelGroup = panelRoot.Find("PanelGroup")?.gameObject;
|
||
if (buttonGroup == null || panelGroup == null) return;
|
||
|
||
panelDict = new Dictionary<string, GameObject>();
|
||
string[] panelNames = { "Selected", "AllUnit", "AllCity", "AllPlayer" };
|
||
|
||
foreach (string name in panelNames)
|
||
{
|
||
Transform panel = panelGroup.transform.Find(name);
|
||
if (panel != null)
|
||
{
|
||
panelDict[name] = panel.gameObject;
|
||
}
|
||
}
|
||
|
||
BindButton("ButtonAllUnit", "AllUnit");
|
||
BindButton("ButtonAllCity", "AllCity");
|
||
BindButton("ButtonAllPlayer", "AllPlayer");
|
||
BindButton("ButtonSelected", "Selected");
|
||
|
||
buttonGroup.SetActive(false);
|
||
panelGroup.SetActive(false);
|
||
foreach (var panel in panelDict.Values)
|
||
panel.SetActive(false);
|
||
|
||
if (!DebugCenter.Instance.DebugMode)
|
||
{
|
||
RODebugUI.SetActive(false);
|
||
}
|
||
}
|
||
|
||
private void ToggleMainPanel()
|
||
{
|
||
bool newState = !buttonGroup.activeSelf;
|
||
buttonGroup.SetActive(newState);
|
||
panelGroup.SetActive(newState);
|
||
}
|
||
|
||
private void ShowOnlyPanel(string key)
|
||
{
|
||
foreach (var kv in panelDict)
|
||
{
|
||
kv.Value.SetActive(kv.Key == key);
|
||
}
|
||
|
||
if (key == "AllUnit")
|
||
{
|
||
|
||
UpdateAllUnitInfo();
|
||
}
|
||
if (key == "AllPlayer")
|
||
{
|
||
|
||
UpdateAllPlayerInfo();
|
||
}
|
||
|
||
}
|
||
|
||
private void BindButton(string buttonName, string panelName)
|
||
{
|
||
Transform btnTransform = buttonGroup.transform.Find(buttonName);
|
||
if (btnTransform == null) return;
|
||
|
||
Button button = btnTransform.GetComponent<Button>();
|
||
if (button == null) return;
|
||
|
||
button.onClick.AddListener(() => ShowOnlyPanel(panelName));
|
||
}
|
||
|
||
public void Update()
|
||
{
|
||
if (!DebugCenter.Instance.DebugMode)
|
||
return;
|
||
if(UIManager.Instance.BottomInfoUI != null && RODebugUI.transform.Find("PanelGroup/Selected").gameObject.activeSelf)
|
||
UIManager.Instance.BottomInfoUI.UpdateDebugInfo();
|
||
//return;
|
||
if (_mapData.PlayerMap.SelfPlayerData.Turn != _turn)
|
||
{
|
||
_turn = _mapData.PlayerMap.SelfPlayerData.Turn;
|
||
UpdateAllPlayerInfo();
|
||
}
|
||
|
||
|
||
}
|
||
|
||
public void UpdateAllUnitInfo()
|
||
{
|
||
var c = RODebugUI.transform.Find("PanelGroup/AllUnit/Scroll View/Viewport/Content");
|
||
foreach (Transform child in c)
|
||
GameObject.Destroy(child.gameObject);
|
||
foreach (var unit in _mapData.UnitMap.UnitList)
|
||
{
|
||
GameObject textObj = new GameObject("TMP_Child");
|
||
|
||
// 设置其父对象
|
||
textObj.transform.SetParent(c, false); // false 保留局部坐标
|
||
|
||
// 添加 RectTransform(UI 元素需要)
|
||
RectTransform rectTransform = textObj.AddComponent<RectTransform>();
|
||
rectTransform.anchorMin = new Vector2(0.5f, 0.5f);
|
||
rectTransform.anchorMax = new Vector2(0.5f, 0.5f);
|
||
rectTransform.anchoredPosition = Vector2.zero;
|
||
|
||
// 添加 TextMeshProUGUI 组件
|
||
TextMeshProUGUI tmp = textObj.AddComponent<TextMeshProUGUI>();
|
||
tmp.text = "UnitID = " + unit.Id;
|
||
tmp.fontSize = 36;
|
||
tmp.alignment = TextAlignmentOptions.Center;
|
||
tmp.color = Color.white;
|
||
}
|
||
}
|
||
|
||
public void UpdateAllPlayerInfo()
|
||
{
|
||
var c = RODebugUI.transform.Find("PanelGroup/AllPlayer/Scroll View/Viewport/Content");
|
||
foreach (Transform child in c)
|
||
GameObject.Destroy(child.gameObject);
|
||
|
||
foreach (var player in _mapData.PlayerMap.PlayerDataList)
|
||
{
|
||
GameObject textObj = new GameObject("TMP_Child");
|
||
|
||
// 设置其父对象
|
||
textObj.transform.SetParent(c, false); // false 保留局部坐标
|
||
|
||
// 添加 RectTransform(UI 元素需要)
|
||
RectTransform rectTransform = textObj.AddComponent<RectTransform>();
|
||
rectTransform.anchorMin = new Vector2(0.5f, 0.5f);
|
||
rectTransform.anchorMax = new Vector2(0.5f, 0.5f);
|
||
rectTransform.anchoredPosition = Vector2.zero;
|
||
|
||
// 添加 TextMeshProUGUI 组件
|
||
TextMeshProUGUI tmp = textObj.AddComponent<TextMeshProUGUI>();
|
||
tmp.text = $"-------- ID={player.Id}, Civ={player.PlayerCivId}, Force={player.PlayerForceId} --------\n";
|
||
tmp.text += $"Money = {player.PlayerWealth}, Stratery = ???\n";
|
||
tmp.text += $"Tech:";
|
||
foreach (var tech in player.TechTree.TechSet)
|
||
{
|
||
tmp.text += tech + " ";
|
||
}
|
||
tmp.text += "\n --- \n";
|
||
tmp.fontSize = 24;
|
||
tmp.alignment = TextAlignmentOptions.Center;
|
||
tmp.color = Color.white;
|
||
|
||
var fitter = textObj.AddComponent<ContentSizeFitter>();
|
||
fitter.verticalFit = ContentSizeFitter.FitMode.PreferredSize;
|
||
//fitter.horizontalFit = ContentSizeFitter.FitMode.Unconstrained;*/
|
||
}
|
||
}
|
||
|
||
}
|