保存Unity中的Log日志

编程入门 行业动态 更新时间:2024-10-24 19:21:12

保存Unity中的Log<a href=https://www.elefans.com/category/jswz/34/1770796.html style=日志"/>

保存Unity中的Log日志

代码中的debug日志保存本地

using System.Collections;
using UnityEngine;
using System.IO;public class SaveLog : MonoBehaviour
{private float length;Queue queue;private void Awake(){DontDestroyOnLoad(this);LogToFile("Version of the runtime: " + Application.unityVersion, true, false);Application.logMessageReceivedThreaded += OnReceiveLogMsg;queue = new Queue();}// Start is called before the first frame updatevoid OnReceiveLogMsg(string condition, string stackTrace, LogType type) {string _type = "";switch (type){case LogType.Error:_type = "error";break;case LogType.Assert:_type = "Assert";break;case LogType.Warning:_type = "Warning";break;case LogType.Log:_type = "Log";break;case LogType.Exception:_type = "Exception";break;default:break;}string msg = "[MSG]:" + condition + "--" + "[station]:" + stackTrace + "-" + "[LogType]:" + _type;queue.Enqueue(msg);}// Update is called once per framevoid Update(){CheckLogs();}void CheckLogs() {if (queue.Count != 0) {LogToFile(queue.Peek().ToString(), true, true,()=> { queue.Dequeue(); });           }}public  void LogToFile(string str, bool bwithTime, bool bAppendLineFeed,System.Action callback = null) {if (str == null) return;try{
#if UNITY_EDITORstring fname = Application.dataPath + "/Unitylog.txt";
#elsestring fname = Application.persistentDataPath+ "/Unitylog.txt";
#endifif (fname == "" || fname == null) return;StreamWriter writer = new StreamWriter(fname, true, System.Text.Encoding.Default);if (bwithTime) writer.WriteLine("\r\n\r\n---------" + System.DateTime.Now.ToString());if (bAppendLineFeed) writer.WriteLine(str);else writer.Write(str);writer.Close();callback?.Invoke();}catch{throw;}}
}

结果:

更多推荐

保存Unity中的Log日志

本文发布于:2023-07-01 07:33:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/972683.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日志   Unity   Log

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!