1
This commit is contained in:
@@ -6,6 +6,7 @@ using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
@@ -208,6 +209,9 @@ public class GameManager : MonoSingleton<GameManager>
|
||||
// ⭐ 防止手机屏幕自动黑屏休眠
|
||||
Screen.sleepTimeout = SleepTimeout.NeverSleep;
|
||||
|
||||
// ⭐ 启动日志文件自动保存:把所有 Debug.Log/Warning/Error 写入 persistentDataPath/Logs/
|
||||
InitLogFileWriter();
|
||||
|
||||
AudioResourcesData audioResourcesData = new AudioResourcesData();
|
||||
InItLoad();
|
||||
//TurretResourcesData turretResourcesData = new TurretResourcesData();
|
||||
@@ -239,6 +243,59 @@ public class GameManager : MonoSingleton<GameManager>
|
||||
APIRequestList.GetActivityConfig(res => { activityConfig = res; });
|
||||
|
||||
}
|
||||
|
||||
// ⭐ 日志文件写入器:把 Application.logMessageReceived 的日志实时写入文件
|
||||
private StreamWriter _logFileWriter;
|
||||
private void InitLogFileWriter()
|
||||
{
|
||||
try
|
||||
{
|
||||
string logDir = Path.Combine(Application.persistentDataPath, "Logs");
|
||||
if (!Directory.Exists(logDir)) Directory.CreateDirectory(logDir);
|
||||
string logPath = Path.Combine(logDir, DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss") + ".log");
|
||||
_logFileWriter = new StreamWriter(logPath, true) { AutoFlush = true };
|
||||
Application.logMessageReceived += OnLogMessageReceived;
|
||||
Debug.Log($"[GameManager] 日志文件已创建: {logPath}");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogError($"[GameManager] 初始化日志文件失败: {e.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private void OnLogMessageReceived(string condition, string stackTrace, LogType type)
|
||||
{
|
||||
if (_logFileWriter == null) return;
|
||||
try
|
||||
{
|
||||
string prefix = type switch
|
||||
{
|
||||
LogType.Error => "[ERROR]",
|
||||
LogType.Assert => "[ASSERT]",
|
||||
LogType.Warning => "[WARN]",
|
||||
LogType.Log => "[LOG]",
|
||||
LogType.Exception => "[EXCEPTION]",
|
||||
_ => "[LOG]"
|
||||
};
|
||||
_logFileWriter.WriteLine($"{DateTime.Now:HH:mm:ss.fff} {prefix} {condition}");
|
||||
if (type == LogType.Error || type == LogType.Exception)
|
||||
{
|
||||
_logFileWriter.WriteLine($" Stack: {stackTrace}");
|
||||
}
|
||||
}
|
||||
catch { /* 写日志失败不影响游戏运行 */ }
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
if (_logFileWriter != null)
|
||||
{
|
||||
Application.logMessageReceived -= OnLogMessageReceived;
|
||||
try { _logFileWriter.Flush(); _logFileWriter.Close(); } catch { }
|
||||
_logFileWriter = null;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task InItLoad()
|
||||
{
|
||||
await TurretResourcesData.LoadTurretAssetsData();
|
||||
|
||||
Reference in New Issue
Block a user