亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Unity實(shí)現(xiàn)簡(jiǎn)易日志輸出功能

 更新時(shí)間:2019年09月17日 09:21:39   作者:Blinkedu  
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)簡(jiǎn)易日志輸出功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

在使用Unity中的Debug.Log()進(jìn)行日志輸出時(shí)很不方便,在打包出來的可執(zhí)行文件中沒有辦法看到輸出,所有就想自己實(shí)現(xiàn)一個(gè)簡(jiǎn)易的日志輸出功能,可以輸出到日志文件,因?yàn)槟芰?shí)在是不夠,所以有錯(cuò)誤和不合理的地方,還請(qǐng)各位老師指點(diǎn)一下,謝謝啦

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>
 /// 構(gòu)造方法
 /// </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.日志系統(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() { }
 
 /// <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.測(cè)試 

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();
 }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C#中數(shù)組、ArrayList、List、Dictionary的用法與區(qū)別淺析(存取數(shù)據(jù))

    C#中數(shù)組、ArrayList、List、Dictionary的用法與區(qū)別淺析(存取數(shù)據(jù))

    在工作中經(jīng)常遇到C#數(shù)組、ArrayList、List、Dictionary存取數(shù)據(jù),但是該選擇哪種類型進(jìn)行存儲(chǔ)數(shù)據(jù)呢?很迷茫,今天小編抽空給大家整理下這方面的內(nèi)容,需要的朋友參考下吧
    2017-02-02
  • 基于C#委托的深入分析

    基于C#委托的深入分析

    本篇文章介紹了,基于C#委托的深入分析。需要的朋友參考下
    2013-04-04
  • CefSharp過濾圖片RequestHandler問題

    CefSharp過濾圖片RequestHandler問題

    這篇文章主要介紹了CefSharp過濾圖片RequestHandler問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • C#設(shè)計(jì)模式之外觀模式介紹

    C#設(shè)計(jì)模式之外觀模式介紹

    外觀模式:為子系統(tǒng)中的一組接口提供一個(gè)一致的界面,此模式定義了一個(gè)高層的接口,這個(gè)借口使得這子系統(tǒng)容易使用
    2012-10-10
  • C#用websocket實(shí)現(xiàn)簡(jiǎn)易聊天功能(客戶端)

    C#用websocket實(shí)現(xiàn)簡(jiǎn)易聊天功能(客戶端)

    這篇文章主要為大家詳細(xì)介紹了C#用websocket實(shí)現(xiàn)簡(jiǎn)易聊天功能,客戶端方向,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • Unity接入百度AI實(shí)現(xiàn)果蔬識(shí)別

    Unity接入百度AI實(shí)現(xiàn)果蔬識(shí)別

    本文將介紹如何利用Unity接入百度AI從而實(shí)現(xiàn)果蔬識(shí)別,可以做到識(shí)別近千種水果和蔬菜的名稱,可自定義返回識(shí)別結(jié)果數(shù)。感興趣的小伙伴可以了解一下
    2022-02-02
  • C#引用類型和值類型的介紹與實(shí)例

    C#引用類型和值類型的介紹與實(shí)例

    這篇文章主要介紹了C#引用類型和值類型,有需要的朋友可以參考一下
    2013-12-12
  • C# 委托(跨窗體操作控件)實(shí)例流程講解

    C# 委托(跨窗體操作控件)實(shí)例流程講解

    今天研究了一下,在C#里面卻是可以不用自定義消息這么復(fù)雜的方法來實(shí)現(xiàn)跨窗體調(diào)用控件,C#有更好的辦法就是委托。
    2013-03-03
  • 深入理解C# 委托與事件

    深入理解C# 委托與事件

    本文主要介紹了深入理解C# 委托與事件,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2008-05-05
  • WinForm限制窗體不能移到屏幕外的方法

    WinForm限制窗體不能移到屏幕外的方法

    這篇文章主要介紹了WinForm限制窗體不能移到屏幕外的方法,實(shí)例分析了C#中WinForm窗體操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-08-08

最新評(píng)論