这期内容当中小编将会给大家带来有关Unity中怎么实现日志输出功能,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
站在用户的角度思考问题,与客户深入沟通,找到开州网站设计与开州网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:成都网站设计、网站制作、企业官网、英文网站、手机端网站、网站推广、申请域名、虚拟空间、企业邮箱。业务覆盖开州地区。
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; /// /// 构造方法 /// /// 是否清空原有的日志 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() { } /// /// 初始化 /// /// 日志输出器 /// 是否开启日志输出 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; } /// /// 过滤器 /// /// 需要显示的日志类型 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(); }}
上述就是小编为大家分享的Unity中怎么实现日志输出功能了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。
网页名称:Unity中怎么实现日志输出功能
本文路径:
http://mswzjz.cn/article/ipijsj.html