Files
gold_dolphin/unity/Assets/UI/DebugResultButtons.cs
T
2026-07-04 14:45:52 +08:00

173 lines
6.6 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.SceneManagement;
using UnityEngine.UI;
namespace GameFramework
{
/// <summary>
/// 调试工具 —— 在 Gameplay 场景中显示"胜利"和"失败"按钮,
/// 点击后直接触发 GameManager.Win() 或 GameManager.GameOver()。
/// 测试完成后删除此脚本。
/// </summary>
public class DebugResultButtons : MonoBehaviour
{
private string _debugLog = "";
private GameResultScreen _resultScreen;
// 转场效果
private bool _isFading = false;
private float _fadeAlpha = 0f;
private float _fadeSpeed = 2f; // 淡出速度
private string _pendingScene = "";
private Texture2D _fadeTexture;
private void Start()
{
// 查找场景中的 GameResultScreen
_resultScreen = FindObjectOfType<GameResultScreen>();
if (_resultScreen == null)
{
Debug.LogWarning("[DebugResultButtons] 场景中没有 GameResultScreen");
}
else
{
Debug.Log("[DebugResultButtons] 找到 GameResultScreen: " + _resultScreen.gameObject.name);
}
// 检查 SceneLoader
if (SceneLoader.Instance == null)
{
Debug.LogWarning("[DebugResultButtons] SceneLoader.Instance 为 null,将使用 SceneManager 直接加载场景");
}
// 创建用于淡入淡出的纹理
_fadeTexture = new Texture2D(1, 1);
_fadeTexture.SetPixel(0, 0, Color.black);
_fadeTexture.Apply();
}
private void Update()
{
// 处理淡出效果
if (_isFading)
{
_fadeAlpha += Time.deltaTime * _fadeSpeed;
if (_fadeAlpha >= 1f)
{
_fadeAlpha = 1f;
// 淡出完成,加载场景
if (!string.IsNullOrEmpty(_pendingScene))
{
Debug.Log($"[DebugResultButtons] 淡出完成,加载场景: {_pendingScene}");
SceneManager.LoadScene(_pendingScene);
}
_isFading = false;
}
}
}
private void OnGUI()
{
// 左上角显示调试信息(大字体,黄色)
GUIStyle debugStyle = new GUIStyle(GUI.skin.label);
debugStyle.fontSize = 18;
debugStyle.normal.textColor = Color.yellow;
debugStyle.fontStyle = FontStyle.Bold;
// 调试信息背景
GUI.Box(new Rect(5, 5, 500, 130), "");
GUI.Label(new Rect(10, 10, 480, 25), $"GameManager.Instance: {(GameManager.Instance != null ? "【存在】" : "null - 缺失!")}", debugStyle);
GUI.Label(new Rect(10, 35, 480, 25), $"GameState: {GameManager.GameState}", debugStyle);
string resultScreenStatus = _resultScreen != null ? "【已找到】" : "【未找到!】";
GUI.Label(new Rect(10, 60, 480, 25), $"GameResultScreen: {resultScreenStatus}", debugStyle);
if (!string.IsNullOrEmpty(_debugLog))
{
GUIStyle logStyle = new GUIStyle(GUI.skin.label);
logStyle.fontSize = 16;
logStyle.normal.textColor = _debugLog.Contains("错误") ? Color.red : Color.green;
logStyle.fontStyle = FontStyle.Bold;
GUI.Label(new Rect(10, 85, 580, 25), _debugLog, logStyle);
}
// 右上角显示两个按钮(淡出时隐藏)
if (!_isFading)
{
float x = Screen.width - 220;
float y = 10;
GUIStyle style = new GUIStyle(GUI.skin.button);
style.fontSize = 20;
style.fontStyle = FontStyle.Bold;
// 胜利按钮(绿色)
GUI.backgroundColor = new Color(0.2f, 0.8f, 0.2f, 0.9f);
if (GUI.Button(new Rect(x, y, 100, 50), "胜利", style))
{
if (GameManager.Instance == null)
{
_debugLog = "[错误] GameManager.Instance 为 null";
Debug.LogError("[DebugResultButtons] GameManager.Instance 为 null");
}
else
{
_debugLog = $"[调用] Win() - 时间: {Time.time:F2}";
Debug.Log("[DebugResultButtons] 调用 GameManager.Win()");
GameManager.Win();
// 如果没有 ResultScreen,直接跳转结算场景
if (_resultScreen == null)
{
StartFadeToScene("Scoring");
}
}
}
// 失败按钮(红色)
GUI.backgroundColor = new Color(0.8f, 0.2f, 0.2f, 0.9f);
if (GUI.Button(new Rect(x + 110, y, 100, 50), "失败", style))
{
if (GameManager.Instance == null)
{
_debugLog = "[错误] GameManager.Instance 为 null";
Debug.LogError("[DebugResultButtons] GameManager.Instance 为 null");
}
else
{
_debugLog = $"[调用] GameOver() - 时间: {Time.time:F2}";
Debug.Log("[DebugResultButtons] 调用 GameManager.GameOver()");
GameManager.GameOver();
// 如果没有 ResultScreen,直接跳转结算场景
if (_resultScreen == null)
{
StartFadeToScene("Scoring");
}
}
}
// 重置颜色
GUI.backgroundColor = Color.white;
}
// 绘制淡出遮罩
if (_isFading || _fadeAlpha > 0)
{
GUI.color = new Color(0, 0, 0, _fadeAlpha);
GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), _fadeTexture);
GUI.color = Color.white;
}
}
private void StartFadeToScene(string sceneName)
{
_debugLog = $"[转场] 淡出到 {sceneName}...";
_pendingScene = sceneName;
_isFading = true;
_fadeAlpha = 0f;
}
}
}