前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Unity封装定义自己喜欢的Log类型

Unity封装定义自己喜欢的Log类型

作者头像
bering
发布2019-12-03 15:53:05
9960
发布2019-12-03 15:53:05
举报
文章被收录于专栏:游戏开发之旅游戏开发之旅

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/CJB_King/article/details/88593909

Unity封装定义自己喜欢的Log类型

Unity自己的Debug.Log本身的打印模式太单一,但是Unity的Log系统支持标签识别

支持的标签:(粗体斜体大小颜色项支持Debug.Log)

b 粗体 :<b>text</b>

i 斜体 :<i>text</i>

size大小 :<size=10>text</size> 这个标签是Debug.Log看得到的

color颜色:<color=#00ffffff>text</color> 字母对应于16进制数字,表示红绿蓝和透明度 ;<color=red>text</color> 使用颜色名称,总是假定完成不透明

为了查看日志时便于区分,我们这里自己封装一个类,固定的颜色显示对应的日志,也可以自己定义一种日志类型对应一种颜色,这样查看日志就不会那么单一无趣了,打开VS,新建一个MyDebug类,完成后,我们把它生成dll文件,以便以后快速集成到我们的开发中去,生成导入Unity中的dll文件的目标框架是使用.Net FrameWork 3.5,如果选择的框架是framework4.0 会报错。

代码如下:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using UnityEngine;

namespace LoggerSys
{
    public class MyDebug
    {
        public static Dictionary<string, MyDebug> MyDebugs = new Dictionary<string, MyDebug>();

        //是否开启日志系统
        public bool DebugOut = true;

        private string module;
        private string color;

        public string Module
        {
            get
            {
                return this.module;
            }
        }
        private MyDebug(string module, string color)
        {
            this.module = module;
            this.color = color;
        }

        public static MyDebug Create(string module, string color = "black")   //用于创建自己喜欢的Log
        {
            if (MyDebug.MyDebugs.ContainsKey(module))
            {
                return MyDebug.MyDebugs[module];
            }
            MyDebug myDebug = new MyDebug(module, color);
            MyDebug.MyDebugs.Add(module, myDebug);
            return myDebug;
        }
        public void Log(string message)
        {
            if (this.DebugOut)
            {
                string text = string.Format("{0} |{1}|<b><color={2}> {3}|      {4}</color></b>",
                    new object[]{
                        DateTime.Now.ToShortTimeString(),
                        "INFO",
                        this.color,
                        this.module,
                        message
                    });
                Debug.Log(text);  //Unity引擎使用
            }
        }

        public void LogError(object message)
        {
            if (this.DebugOut)
            {
                string text = string.Format("{0} |{1}|<color=red>{2}|   {3}</color>",
                    new object[]{
                        DateTime.Now.ToShortTimeString(),
                        "ERROR",
                        this.module,
                        message
                    });
                Debug.LogError(text);  //Unity引擎使用
            }
        }

        public void LogException(Exception exception)
        {
            if (this.DebugOut)
            {
                Debug.LogException(exception);
            }
        }

        public void LogWarning(object message)
        {
            if (this.DebugOut)
            {
                string text = string.Format("{0} |{1}|<color=yellow><b>{2}|    {3}</b></color>", new object[]
				{
					DateTime.Now.ToShortTimeString(),
					"WARNING",
					this.module,
					message
				});
                Debug.LogWarning(text);
            }
        }

        public void LogFormat(string format, params object[] args)
        {
            if (this.DebugOut)
            {
                string text = string.Format("{0} |{1}<b><color=yellow>{2}|   {3}</color></b>", new object[]
                {
                   DateTime.Now.ToShortTimeString(),
                   "INFO",
                   this.module,
                   format
                });
                Debug.LogFormat(text, args);
            }
        }
        public void LogErrorFormat(string format, params object[] args)
        {
            if (this.DebugOut)
            {
                string text = string.Format("{0} |{1}|<color=red>{2}|    {3}</color>", new object[]
				{
					DateTime.Now.ToShortTimeString(),
					"ERROR",
					this.module,
					format
				});
                Debug.LogErrorFormat(text, args);
            }
        }
        public void LogWarningFormat(string format, params object[] args)
        {
            if (this.DebugOut)
            {
                string text = string.Format("{0} |{1}|<b><color=yellow>{2}|    {3}</color></b>", new object[]
				{
					DateTime.Now.ToShortTimeString(),
					"WARNING",
					this.module,
					format
				});
                Debug.LogErrorFormat(text, args);
            }
        }
        public static MyDebug Sys = MyDebug.Create("SYS", "#000000ff");


        public static MyDebug Res = MyDebug.Create("RES", "#008000ff");


        public static MyDebug Net = MyDebug.Create("NET", "#add8e6ff");


        public static MyDebug UI = MyDebug.Create("UI", "#008080ff");


    }
}

效果图如下:

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-03-16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档