MeowmentDebugTool/Packages/com.bywaystudios.meowmentdebugtool/Runtime/SettingsModule.cs
zhang hongbo cf352f2a22 11
2026-04-06 10:03:28 +08:00

222 lines
8.4 KiB
C#
Raw Permalink 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 UnityEngine.UI;
using TMPro;
namespace MeowmentDebugTool
{
/// <summary>
/// 设置模块 - 提供窗口全屏布局与缓存池设置功能
/// </summary>
public class SettingsModule : IDebugModule
{
#region
private GameObject settingsPage;
private TMP_InputField widthInputField;
private TMP_InputField heightInputField;
private Button applyResolutionButton;
private Button resetResolutionButton;
private TMP_Text currentResolutionText;
// Console Log缓存池设置
private TMP_InputField infoBufferInputField;
private TMP_InputField warningBufferInputField;
private TMP_InputField errorBufferInputField;
private TMP_InputField fatalBufferInputField;
private Button applyBufferButton;
private Button resetBufferButton;
private ConsoleModule consoleModule;
// 主窗口引用
private RectTransform mainWindow;
private Vector2 detectedScreenResolution;
// 默认缓存池大小
private const int DEFAULT_BUFFER_SIZE = 50;
#endregion
#region
public SettingsModule(GameObject page, TMP_InputField widthInput, TMP_InputField heightInput,
Button applyButton, Button resetButton, TMP_Text resolutionText,
TMP_InputField infoBufferInput, TMP_InputField warningBufferInput,
TMP_InputField errorBufferInput, TMP_InputField fatalBufferInput,
Button applyBufferBtn, Button resetBufferBtn,
RectTransform mainWin)
{
settingsPage = page;
widthInputField = widthInput;
heightInputField = heightInput;
applyResolutionButton = applyButton;
resetResolutionButton = resetButton;
currentResolutionText = resolutionText;
infoBufferInputField = infoBufferInput;
warningBufferInputField = warningBufferInput;
errorBufferInputField = errorBufferInput;
fatalBufferInputField = fatalBufferInput;
applyBufferButton = applyBufferBtn;
resetBufferButton = resetBufferBtn;
mainWindow = mainWin;
}
#endregion
#region IDebugModule
public void Initialize()
{
Debug.Log("[SettingsModule] 初始化设置模块...");
if (widthInputField != null)
widthInputField.readOnly = true;
if (heightInputField != null)
heightInputField.readOnly = true;
if (applyResolutionButton != null)
applyResolutionButton.interactable = false;
if (resetResolutionButton != null)
resetResolutionButton.onClick.AddListener(RefreshAutoResolution);
if (applyBufferButton != null)
applyBufferButton.onClick.AddListener(ApplyBufferSize);
if (resetBufferButton != null)
resetBufferButton.onClick.AddListener(ResetBufferSize);
// 自动识别并应用当前屏幕全屏布局
RefreshAutoResolution();
// 设置默认缓存池大小
ResetBufferSize();
}
public GameObject GetPage()
{
return settingsPage;
}
public string GetModuleName()
{
return "设置";
}
/// <summary>
/// 重新识别并应用当前屏幕分辨率
/// </summary>
public void RefreshAutoResolution()
{
detectedScreenResolution = new Vector2(Screen.width, Screen.height);
UpdateResolutionInputs(detectedScreenResolution);
ApplyResolution();
Debug.Log($"[SettingsModule] 已刷新设备分辨率: {detectedScreenResolution.x} x {detectedScreenResolution.y}调试窗口使用全屏拉伸布局不处理SafeArea");
}
/// <summary>
/// 设置 Console Module 引用
/// </summary>
public void SetConsoleModule(ConsoleModule module)
{
consoleModule = module;
// 更新输入框显示当前值
if (consoleModule != null)
{
consoleModule.GetMaxLogLines(out int maxInfo, out int maxWarning, out int maxError, out int maxFatal);
if (infoBufferInputField != null)
infoBufferInputField.text = maxInfo.ToString();
if (warningBufferInputField != null)
warningBufferInputField.text = maxWarning.ToString();
if (errorBufferInputField != null)
errorBufferInputField.text = maxError.ToString();
if (fatalBufferInputField != null)
fatalBufferInputField.text = maxFatal.ToString();
}
}
#endregion
#region
private void ApplyResolution()
{
FitMainWindowToFullScreen();
if (currentResolutionText != null)
{
currentResolutionText.text = $"设备: {detectedScreenResolution.x:0} x {detectedScreenResolution.y:0} / 调试窗口: 全屏拉伸";
}
// 不需要强制刷新Canvas会导致rebuild loop
// Canvas会自动在下一帧更新
}
private void UpdateResolutionInputs(Vector2 resolution)
{
if (widthInputField != null)
widthInputField.text = Mathf.RoundToInt(resolution.x).ToString();
if (heightInputField != null)
heightInputField.text = Mathf.RoundToInt(resolution.y).ToString();
}
private void FitMainWindowToFullScreen()
{
if (mainWindow == null)
return;
mainWindow.anchorMin = Vector2.zero;
mainWindow.anchorMax = Vector2.one;
mainWindow.pivot = new Vector2(0.5f, 0.5f);
mainWindow.anchoredPosition = Vector2.zero;
mainWindow.sizeDelta = Vector2.zero;
mainWindow.offsetMin = Vector2.zero;
mainWindow.offsetMax = Vector2.zero;
}
private void ApplyBufferSize()
{
if (consoleModule == null)
{
Debug.LogWarning("[SettingsModule] ConsoleModule未设置无法应用缓存池大小");
return;
}
int maxInfo = DEFAULT_BUFFER_SIZE;
int maxWarning = DEFAULT_BUFFER_SIZE;
int maxError = DEFAULT_BUFFER_SIZE;
int maxFatal = DEFAULT_BUFFER_SIZE;
if (infoBufferInputField != null && int.TryParse(infoBufferInputField.text, out int info) && info > 0)
maxInfo = info;
if (warningBufferInputField != null && int.TryParse(warningBufferInputField.text, out int warning) && warning > 0)
maxWarning = warning;
if (errorBufferInputField != null && int.TryParse(errorBufferInputField.text, out int error) && error > 0)
maxError = error;
if (fatalBufferInputField != null && int.TryParse(fatalBufferInputField.text, out int fatal) && fatal > 0)
maxFatal = fatal;
consoleModule.SetMaxLogLines(maxInfo, maxWarning, maxError, maxFatal);
Debug.Log($"[SettingsModule] 已应用缓存池大小 - Info:{maxInfo}, Warning:{maxWarning}, Error:{maxError}, Fatal:{maxFatal}");
}
private void ResetBufferSize()
{
if (infoBufferInputField != null)
infoBufferInputField.text = DEFAULT_BUFFER_SIZE.ToString();
if (warningBufferInputField != null)
warningBufferInputField.text = DEFAULT_BUFFER_SIZE.ToString();
if (errorBufferInputField != null)
errorBufferInputField.text = DEFAULT_BUFFER_SIZE.ToString();
if (fatalBufferInputField != null)
fatalBufferInputField.text = DEFAULT_BUFFER_SIZE.ToString();
if (consoleModule != null)
{
consoleModule.SetMaxLogLines(DEFAULT_BUFFER_SIZE, DEFAULT_BUFFER_SIZE, DEFAULT_BUFFER_SIZE, DEFAULT_BUFFER_SIZE);
Debug.Log($"[SettingsModule] 已重置缓存池大小为默认值: {DEFAULT_BUFFER_SIZE}");
}
}
#endregion
}
}