封装系统自带的Debug

  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("<color=green>[SKILL]</color>---" + message, context);
            }
        }
        static public void LogWarning(object message)
        {
            LogWarning(message, null);
        }
        static public void LogWarning(object message, UnityEngine.Object context)
        {
            if (Enable)
            {
                Debug.LogWarning("<color=blue>[SKILL]</color>---" + message, context);
            }
        }
        static public void LogError(object message)
        {
            LogError(message, null);
        }
        static public void LogError(object message, UnityEngine.Object context)
        {
            if (Enable)
            {
                Debug.LogError("<color=red>[SKILL]</color>---" + message, context);
            }
        }

    }
}

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

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

原文地址:https://www.cnblogs.com/sifenkesi/p/4194329.html