优化死亡界面跳转

This commit is contained in:
2026-07-04 15:50:39 +08:00
parent c8e66f72b1
commit 3394e0a4e9
5 changed files with 207 additions and 133 deletions
+39 -128
View File
@@ -1,6 +1,4 @@
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
namespace GameFramework
{
@@ -12,59 +10,6 @@ namespace GameFramework
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()
{
@@ -75,98 +20,64 @@ namespace GameFramework
debugStyle.fontStyle = FontStyle.Bold;
// 调试信息背景
GUI.Box(new Rect(5, 5, 500, 130), "");
GUI.Box(new Rect(5, 5, 500, 85), "");
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.normal.textColor = Color.green;
logStyle.fontStyle = FontStyle.Bold;
GUI.Label(new Rect(10, 85, 580, 25), _debugLog, logStyle);
GUI.Label(new Rect(10, 60, 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))
{
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)
{
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");
}
}
_debugLog = "[错误] GameManager.Instance 为 null";
Debug.LogError("[DebugResultButtons] GameManager.Instance 为 null");
}
// 失败按钮(红色)
GUI.backgroundColor = new Color(0.8f, 0.2f, 0.2f, 0.9f);
if (GUI.Button(new Rect(x + 110, y, 100, 50), "失败", style))
else
{
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");
}
}
_debugLog = $"[调用] Win() - 时间: {Time.time:F2}";
Debug.Log("[DebugResultButtons] 调用 GameManager.Win()");
GameManager.Win();
}
// 重置颜色
GUI.backgroundColor = Color.white;
}
// 绘制淡出遮罩
if (_isFading || _fadeAlpha > 0)
// 失败按钮(红色)
GUI.backgroundColor = new Color(0.8f, 0.2f, 0.2f, 0.9f);
if (GUI.Button(new Rect(x + 110, y, 100, 50), "失败", style))
{
GUI.color = new Color(0, 0, 0, _fadeAlpha);
GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), _fadeTexture);
GUI.color = Color.white;
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();
}
}
}
private void StartFadeToScene(string sceneName)
{
_debugLog = $"[转场] 淡出到 {sceneName}...";
_pendingScene = sceneName;
_isFading = true;
_fadeAlpha = 0f;
// 重置颜色
GUI.backgroundColor = Color.white;
}
}
}