博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
封装系统自带的Debug
阅读量:7106 次
发布时间:2019-06-28

本文共 4677 字,大约阅读时间需要 15 分钟。

  Unity3d的Debug.Log函数用于打印日志,一般项目中都会对其作如下两件事情:

  (1)希望有一个总的开关来控制整个游戏中日志的打印与否;

  (2)有的系统会将Log封一层并添加统一的标记,比如Skill模块希望打印的日志格式为[Skill]***。

  对于第一个问题,Unity没有统一的开关用于控制日志的输出。对于第二个问题,将Log用一个函数封一层,那么在调试的时候双击Console里面的堆栈时将不能跳转到相应的逻辑块,很麻烦。

  怎么解决呢,有一个办法,就是将Debug.Log封装进一个DLL,话不多说代码如下所示:

using System;using UnityEngine;namespace MyDebug{    public class Debug    {        static public bool Enable = false;        static public void Log(object message)        {            Log(message, null);        }        static public void Log(object message, UnityEngine.Object context)        {            if (Enable)            {                UnityEngine.Debug.Log(message, context);            }        }        static public void LogWarning(object message)        {            LogWarning(message, null);        }        static public void LogWarning(object message, UnityEngine.Object context)        {            if (Enable)            {                UnityEngine.Debug.LogWarning(message, context);            }        }        static public void LogError(object message)        {            LogError(message, null);        }        static public void LogError(object message, UnityEngine.Object context)        {            if (Enable)            {                UnityEngine.Debug.LogError(message, context);            }        }        public static void LogException(Exception exception)        {            UnityEngine.Debug.LogException(exception);        }        public static void LogException(Exception exception, UnityEngine.Object context)        {            UnityEngine.Debug.LogException(exception, context);        }        public static void DrawLine(Vector3 start, Vector3 end)        {            UnityEngine.Debug.DrawLine(start, end);        }        public static void DrawLine(Vector3 start, Vector3 end, Color color)        {            UnityEngine.Debug.DrawLine(start, end, color);        }        public static void DrawLine(Vector3 start, Vector3 end, Color color, float duration)        {            UnityEngine.Debug.DrawLine(start, end, color, duration);        }        public static void DrawLine(Vector3 start, Vector3 end, Color color, float duration, bool depthTest)        {            UnityEngine.Debug.DrawLine(start, end, color, duration, depthTest);        }        public static void DrawRay(Vector3 start, Vector3 dir)        {            UnityEngine.Debug.DrawRay(start, dir);        }        public static void DrawRay(Vector3 start, Vector3 dir, Color color)        {            UnityEngine.Debug.DrawRay(start, dir, color);        }        public static void DrawRay(Vector3 start, Vector3 dir, Color color, float duration)        {            UnityEngine.Debug.DrawRay(start, dir, color, duration);        }        public static void DrawRay(Vector3 start, Vector3 dir, Color color, float duration, bool depthTest)        {            UnityEngine.Debug.DrawRay(start, dir, color, duration, depthTest);        }        public static void Break()        {            UnityEngine.Debug.Break();        }        public static void ClearDeveloperConsole()        {            UnityEngine.Debug.ClearDeveloperConsole();        }        public static void DebugBreak()        {            UnityEngine.Debug.DebugBreak();        }    }}

  在游戏中有两种用法:

using Debug = MyDebug.Debug;

  然后用法和默认的一样,或者直接使用:

MyDebug.Debug.Log("something");

  同样的,对于第二个问题,可以将Skill系统的Log进行类似的封装:

using System;using System.Collections.Generic;using System.Text;namespace MyDebug{    public class SkillDebug    {        public static bool Enable = true;        static public void Log(object message)        {           Log(message, null);        }        static public void Log(object message, UnityEngine.Object context)        {            if (Enable)            {                Debug.Log("
[SKILL]---" + message, context); } } static public void LogWarning(object message) { LogWarning(message, null); } static public void LogWarning(object message, UnityEngine.Object context) { if (Enable) { Debug.LogWarning("
[SKILL]---" + message, context); } } static public void LogError(object message) { LogError(message, null); } static public void LogError(object message, UnityEngine.Object context) { if (Enable) { Debug.LogError("
[SKILL]---" + message, context); } } }}

  注意,在编译库文件的时候,需要手动地设置工程属性中的目标框架,如下图所示:

  将上述代码编译到MyDebug.dll中,并将此dll放入到游戏工程,那么就可以在游戏中自由使用Log功能,并且有统一开关来控制是否打印日志,而且可以进行直接的堆栈跳转了。

转载地址:http://zaphl.baihongyu.com/

你可能感兴趣的文章
C#中关于系统用户信息持久化(接上文)
查看>>
TestLink学习四:TestLink1.9.13使用说明
查看>>
通用分页存储过程
查看>>
神秘代码让iPhone微信闪退的解决方法
查看>>
文章索引
查看>>
基于jquery结婚电子请柬特效素材
查看>>
Knockout应用开发指南 第九章:高级应用举例
查看>>
EDW on Hadoop(Hadoop上的数据仓库)技术选型和实践思考
查看>>
设计模式——责任链模式
查看>>
hdu 3339 In Action 背包+flyod
查看>>
DevExpress.Build.v14.2
查看>>
需求文档中容易出的错误
查看>>
ViewPagerTransforms
查看>>
一位Erlang程序员的自白
查看>>
移动端遇到的问题
查看>>
一不小心把oschina给戒了
查看>>
互联网数据库架构设计思路
查看>>
酷站收藏
查看>>
编程王道,唯“慢”不破
查看>>
Enterprise Solution 生成实体数据访问接口与实现类型 Code Smith 6.5 模板文件下载
查看>>