MeowmentDebugTool/Packages/com.bywaystudios.meowmentdebugtool/Runtime/SettingsModule.cs
2025-12-22 15:29:55 +08:00

129 lines
4.1 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 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;
// 主窗口引用
private RectTransform mainWindow;
private Canvas canvas;
// 默认分辨率
private Vector2 defaultResolution = new Vector2(1080, 2340);
private Vector2 currentCustomResolution;
#endregion
#region
public SettingsModule(GameObject page, TMP_InputField widthInput, TMP_InputField heightInput,
Button applyButton, Button resetButton, TMP_Text resolutionText,
RectTransform mainWin, Canvas canvasRef)
{
settingsPage = page;
widthInputField = widthInput;
heightInputField = heightInput;
applyResolutionButton = applyButton;
resetResolutionButton = resetButton;
currentResolutionText = resolutionText;
mainWindow = mainWin;
canvas = canvasRef;
currentCustomResolution = defaultResolution;
}
#endregion
#region IDebugModule
public void Initialize()
{
Debug.Log("[SettingsModule] 初始化设置模块...");
if (applyResolutionButton != null)
applyResolutionButton.onClick.AddListener(ApplyCustomResolution);
if (resetResolutionButton != null)
resetResolutionButton.onClick.AddListener(ResetToDefaultResolution);
// 应用默认分辨率
ApplyResolution(defaultResolution);
}
public GameObject GetPage()
{
return settingsPage;
}
public string GetModuleName()
{
return "设置";
}
#endregion
#region
private void ApplyCustomResolution()
{
if (widthInputField == null || heightInputField == null) return;
if (float.TryParse(widthInputField.text, out float width) &&
float.TryParse(heightInputField.text, out float height))
{
if (width > 0 && height > 0)
{
currentCustomResolution = new Vector2(width, height);
ApplyResolution(currentCustomResolution);
Debug.Log($"[SettingsModule] 已应用自定义分辨率: {width} x {height}");
}
else
{
Debug.LogWarning("[SettingsModule] 分辨率值必须大于0");
}
}
else
{
Debug.LogWarning("[SettingsModule] 无效的分辨率值");
}
}
private void ResetToDefaultResolution()
{
currentCustomResolution = defaultResolution;
ApplyResolution(defaultResolution);
if (widthInputField != null)
widthInputField.text = defaultResolution.x.ToString();
if (heightInputField != null)
heightInputField.text = defaultResolution.y.ToString();
Debug.Log($"[SettingsModule] 已重置为默认分辨率: {defaultResolution.x} x {defaultResolution.y}");
}
private void ApplyResolution(Vector2 resolution)
{
if (mainWindow != null)
{
mainWindow.sizeDelta = resolution;
}
if (currentResolutionText != null)
{
currentResolutionText.text = $"当前窗口尺寸: {resolution.x} x {resolution.y}";
}
// 不需要强制刷新Canvas会导致rebuild loop
// Canvas会自动在下一帧更新
}
#endregion
}
}