在使用Unity中的Debug.Log()進行日志輸出時很不方便,在打包出來的可執(zhí)行文件中沒有辦法看到輸出,所有就想自己實現(xiàn)一個簡易的日志輸出功能,可以輸出到日志文件,因為能力實在是不夠,所以有錯誤和不合理的地方,還請各位老師指點一下,謝謝啦
創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè)、榮縣網(wǎng)絡(luò)推廣、微信小程序定制開發(fā)、榮縣網(wǎng)絡(luò)營銷、榮縣企業(yè)策劃、榮縣品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供榮縣建站搭建服務(wù),24小時服務(wù)熱線:18982081108,官方網(wǎng)址:www.cdcxhl.com
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; ////// 構(gòu)造方法 /// /// 是否清空原有的日志 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.日志系統(tǒng)管理類
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(); } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。