如何保存Unity中的Log日志
更新时间:2021年4月10日 00:00 点击:2128
代码中的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 update void 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 frame void 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_EDITOR string fname = Application.dataPath + "/Unitylog.txt"; #else string fname = Application.persistentDataPath+ "/Unitylog.txt"; #endif if (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; } } }
结果:
补充:Unity3D log写入文件
关键代码: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; } } }
以上为个人经验,希望能给大家一个参考,也希望大家多多支持猪先飞。如有错误或未考虑完全的地方,望不吝赐教。
相关文章
- 这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
- 本文主要介绍了python读取和保存mat文件的方法,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-08-25
- 今天小编就为大家分享一篇python 实现将Numpy数组保存为图像,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-04-27
- 这篇文章主要为大家详细介绍了Unity时间戳的使用方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
- 下面我们来看一篇关于安卓开发之保存ImageView中的图片到本地相册教程吧,希望这篇教程能够给大家带来帮助. 代码如下. private void saveImage(ImageView imageVi...2016-11-01
Unity中 ShaderGraph 实现旋涡传送门效果入门级教程(推荐)
通过Twirl 旋转节点对Gradient Noise 梯度噪声节点进行操作,就可得到一个旋转的旋涡效果。具体实现代码跟随小编一起通过本文学习下吧...2021-07-11- 本文主要介绍了4种延时执行的方法,主要包括Update计时器,Invoke,协程,DoTween,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-07-07
- 下面小编就为大家分享一篇C#抓取网络图片保存到本地的实现方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-06-25
- 这篇文章主要为大家详细介绍了Unity shader实现遮罩效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
- 这篇文章主要介绍了unity 判断鼠标是否在哪个UI上的两种实现方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-04-10
- 这篇文章主要为大家详细介绍了Unity实现换装系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-04-11
- 这篇文章主要给大家介绍了关于利用unity代码C#封装为dll的相关资料,文中通过图文将实现的方法介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-25
- 这篇文章主要介绍了unity 实现摄像机绕某点旋转一周,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-04-12
- 这篇文章主要为大家详细介绍了Unity Shader实现径向模糊效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-08-09
- 这篇文章主要为大家详细介绍了Unity实现截图功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
- 这篇文章主要为大家详细介绍了Unity Shader实现2D水流效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
- 这篇文章主要介绍了Unity 按钮添加OnClick事件操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-04-10
- 这篇文章主要为大家详细介绍了Unity使用EzySlice实现模型多边形顺序切割,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-11-03
- 这篇文章主要为大家详细介绍了Unity Shader实现描边OutLine效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
- 这篇文章主要为大家详细介绍了Unity Shader实现裁切效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25