这篇文章主要为大家详细介绍了Unity实现简易日志输出功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
在使用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();
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程学习网。
织梦狗教程
本文标题为:Unity实现简易日志输出功能


基础教程推荐
猜你喜欢
- C#通过标签软件Bartender的ZPL命令打印条码 2023-05-16
- C#中的Linq to JSON操作详解 2023-06-08
- C#调用摄像头实现拍照功能的示例代码 2023-03-09
- C#中 Json 序列化去掉null值的方法 2022-11-18
- Unity 如何获取鼠标停留位置下的物体 2023-04-10
- 实例详解C#实现http不同方法的请求 2022-12-26
- C# 解析XML和反序列化的示例 2023-04-14
- Unity shader实现高斯模糊效果 2023-01-16
- C#获取指定目录下某种格式文件集并备份到指定文件夹 2023-05-30
- c# – USING块在网站与Windows窗体中的行为不同 2023-09-20