溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Unity中怎么實現日志輸出功能

發布時間:2021-08-09 16:36:56 來源:億速云 閱讀:233 作者:Leah 欄目:編程語言

這期內容當中小編將會給大家帶來有關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;   /// <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();  }}

上述就是小編為大家分享的Unity中怎么實現日志輸出功能了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女