Unity实现简易日志输出功能
程序员文章站
2022-06-21 08:46:47
在使用unity中的debug.log()进行日志输出时很不方便,在打包出来的可执行文件中没有办法看到输出,所有就想自己实现一个简易的日志输出功能,可以输出到日志文件,因为能力实在是不...
在使用unity中的debug.log()进行日志输出时很不方便,在打包出来的可执行文件中没有办法看到输出,所有就想自己实现一个简易的日志输出功能,可以输出到日志文件,因为能力实在是不够,所以有错误和不合理的地方,还请各位老师指点一下,谢谢啦
1.日志记录器接口
public interface ilogger { void log(string condition, string stacktrace, unityengine.logtype type); }
2.日志文件记录器
using system; using unityengine; using system.io; public class filelogger : ilogger { private readonly string path; /// <summary> /// 构造方法 /// </summary> /// <param name="isclear">是否清空原有的日志</param> public filelogger(bool isclear = false) { switch (application.platform) { case runtimeplatform.android: path = path.combine( application.persistentdatapath,"log.txt"); break; case runtimeplatform.windowsplayer: path = path.combine(application.datapath, "log.txt"); break; case runtimeplatform.windowseditor: path = path.combine(application.datapath, "log.txt"); break; case runtimeplatform.iphoneplayer: path = path.combine(application.persistentdatapath, "log.txt"); break; case runtimeplatform.osxeditor: break; default: break; } if (isclear) { if (file.exists(path)) { file.delete(path); } } } public void log(string condition, string stacktrace, logtype type) { using (streamwriter sw = new streamwriter(path, true, system.text.encoding.utf8)) { string msg = string.format("[{0}] {1}: {2}\n{3}", getnowtime(), type, condition, stacktrace); sw.writeline(msg); } } #region tool method private string getnowtime() { return datetime.now.tostring("yyyy/mm/dd hh:mm:ss"); } #endregion }
3.日志系统管理类
using system; using unityengine; public class logsys { private static ilogger logger; public static ilogger logger { get { return logger; } } public bool isopen { get { return debug.unitylogger.logenabled; } } private logsys() { } /// <summary> /// 初始化 /// </summary> /// <param name="_logger">日志输出器</param> /// <param name="isopen">是否开启日志输出</param> public static void init(ilogger _logger, bool isopen = true) { init(isopen); logger = _logger; enable(); } public static void init(bool isopen = true) { debug.unitylogger.logenabled = isopen; } /// <summary> /// 过滤器 /// </summary> /// <param name="logtype">需要显示的日志类型</param> public static void filter(logtype logtype = logtype.log) { debug.unitylogger.filterlogtype = logtype; } public static void enable() { if (logger != null) { application.logmessagereceived += logger.log; } } public static void disable() { if (logger != null) { application.logmessagereceived -= logger.log; } } }
4.测试
using system; using system.collections; using system.collections.generic; using system.io; using unityengine; using unityengine.ui; public class test : monobehaviour { public text logtext; void awake() { logsys.init(new filelogger()); } void update() { if (input.getkeydown(keycode.q)) { debug.log("my name is blinkedu."); } if (input.getkeydown(keycode.w)) { debug.logwarning("my name is blinkedu."); } if (input.getkeydown(keycode.e)) { debug.logerror("my name is blinkedu."); } } private void ondestroy() { logsys.disable(); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 简单的php购物车代码