137 lines
4.0 KiB
C#
137 lines
4.0 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;
|
||
|
||
public DebugUI(Main main, MapData mapData)
|
||
{
|
||
_main = main;
|
||
_mapData = mapData;
|
||
|
||
Transform root = _main.UIManager.ROUIManager.transform;
|
||
Transform panelRoot = root.Find("DebugPanel");
|
||
if (panelRoot == null) 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();
|
||
}
|
||
|
||
}
|
||
|
||
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(_main.UIManager.BottomInfoUI != null)
|
||
_main.UIManager.BottomInfoUI.UpdateDebugInfo();
|
||
}
|
||
|
||
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;
|
||
}
|
||
}
|
||
}
|