关键代码:Application.RegisterLogCallback(logCallBack);
using UnityEngine;
using System.IO;
public class Logger
{
string fullPath;
public void InitLogger()
{
fullPath = Application.dataPath + "/output.txt";
if (File.Exists(fullPath)) File.Delete(fullPath);
Debug.Log(fullPath.Replace("/output.txt", ""));
if (Directory.Exists(fullPath.Replace("/output.txt", "")))
{
FileStream fs = File.Create(fullPath);
fs.Close();
Application.logMessageReceived += logCallBack;
}
else
{
Debug.LogError("directory is not exist");
}
}
private void logCallBack(string condition, string stackTrace, LogType type)
{
if (File.Exists(fullPath))
{
using (StreamWriter sw = File.AppendText(fullPath))
{
sw.WriteLine(condition);
sw.WriteLine(stackTrace);
}
}
}
private static Logger s_instance;
public static Logger instance
{
get
{
if (null == s_instance)
s_instance = new Logger();
return s_instance;
}
}
}