在开发过程中避免不了各种Debug调试,在打包的时候为了减少性能消耗,需要将所有的Debug注释或删除掉。如果再次调整则需要取消注释,来回折腾很是麻烦。之前尝试将Debug简单的封装到一个类,利用一个bool控制是否启用Debug功能,正常开发是只需将bool值设为true,打包时设为false即可,但这样就有了更大的一个问题,在控制台双击Log时,只能跳转到封装的Debug类里,不能定位到实际调用的脚本,反而使开发变得更繁琐。这里就需要用到dll文件了,将Debug类封装成dll,可以完美解决以上所有问题~
因为已经有很多的博文对dll封装步骤详细说明了,这里就直接上链接:
仅适用于Windows,在Mac环境下尝试过可惜没成功······
有几点需要注意:
1. 目标框架 要选择.NET Framework 3.5
2. Debug需要引用Unity的UnityEngine.dll,在Unity安装路径下\Editor\Data\Managed\UnityEngine.dll
3. 下面是我封装的代码:
using UnityEngine;
public class Debuger
{
public static bool Enable = true;
public static void Log(object message)
{
if (Enable)
{
Debug.Log(message);
}
}
public static void Log(object message, UnityEngine.Object context)
{
if (Enable)
{
Debug.Log(message, context);
}
}
public static void LogFormat(UnityEngine.Object context, string message, params object[] args)
{
if (Enable)
{
Debug.LogFormat(context, message, args);
}
}
public static void LogFormat(string message, params object[] args)
{
if (Enable)
{
Debug.LogFormat(message, args);
}
}
public static void LogError(object message)
{
if (Enable)
{
Debug.LogError(message);
}
}
public static void LogErrorFormat(UnityEngine.Object context, string message, params object[] args)
{
if (Enable)
{
Debug.LogErrorFormat(context, message, args);
}
}
public static void LogError(object message, UnityEngine.Object context)
{
if (Enable)
{
Debug.LogError(message, context);
}
}
public static void LogWarning(object message)
{
if (Enable)
{
Debug.LogWarning(message);
}
}
public static void LogWarning(object message, UnityEngine.Object context)
{
if (Enable)
{
Debug.LogWarning(message, context);
}
}
public static void LogWarningFormat(string message, params object[] args)
{
if (Enable)
{
Debug.LogWarningFormat(message, args);
}
}
public static void LogWarningFormat(UnityEngine.Object context, string message, params object[] args)
{
if (Enable)
{
Debug.LogWarningFormat(context, message, args);
}
}
public static void Break()
{
if (Enable)
{
Debug.Log("Debuger Break ");
Debug.Break();
}
}
}