public class Test : MonoBehaviour
{
//文件的路径
public string path;
StreamWriter writer;
StreamReader reader;
void Start()
{
SetPath();
// 方法一
FileInfo file = new FileInfo(path);
if (file.Exists)
{
// file.Delete();
// file.Refresh();
}
// 方法二
if (File.Exists(path))
{
File.Delete(path);
}
}
void Update()
{
if (Input.GetMouseButtonUp(0))
{
Debug.Log("Log");
Debug.LogError("LogError");
Debug.LogWarning("LogError");
}
}
void OnEnable()
{
Application.logMessageReceivedThreaded += OnLogMessageReceivedThreaded;
System.AppDomain.CurrentDomain.UnhandledException += _OnUnresolvedExceptionHandler;
}
void OnDisable()
{
Application.logMessageReceivedThreaded -= OnLogMessageReceivedThreaded;
System.AppDomain.CurrentDomain.UnhandledException -= _OnUnresolvedExceptionHandler;
}
private void OnLogMessageReceivedThreaded(string condition, string stackTrace, LogType type)
{
StringBuilder str = new StringBuilder();
str.Append(type.ToString() + ":" + condition + " 堆栈信息:" + stackTrace);
// WriteIntoTxt(str.ToString());
WriteIntoTxtTown(str.ToString());
}
private void _OnUnresolvedExceptionHandler(object sender, UnhandledExceptionEventArgs e)
{
Debug.LogError(sender);
}
// 方法一
public void WriteIntoTxt(string message)
{
FileInfo file = new FileInfo(path);
if (!file.Exists)
{
writer = file.CreateText();
}
else
{
writer = file.AppendText();
}
writer.WriteLine(message);
writer.Flush();
writer.Dispose();
writer.Close();
}
// 方法二
public void WriteIntoTxtTown(string message)
{
File.AppendAllText(path, message);
}
void SetPath()
{
if (Application.platform == RuntimePlatform.Android)
{
path = Application.persistentDataPath + "/logInfo.txt";
}
if (Application.platform == RuntimePlatform.WindowsEditor)
{
path = Application.streamingAssetsPath + "/logInfo.txt";
}
}
}
网友评论