Unity Log封装
程序员文章站
2022-07-04 20:19:36
...
重新封装Log的意义:
- 用更少的字符,方便快速编写Code
- 统一管理Log是否起作用
封装的Log类型
- Debug.Log 正常Log
- Debug.LogWarning 警告
- Debug.LogError 错误
- Debug.Break 暂停游戏
- Debug.Assert 断言(条件为false时输出)
封装效果
代码
using UnityEngine;
namespace Assets.Scripts
{
public class LogUtil
{
private static bool D = false;
private static bool W = false;
private static bool E = false;
private static bool B = false;
private static bool A = false;
private static void Initialize(bool isOpen)
{
A = B = D = W = E = isOpen;
}
public static void d(string message)
{
if (D)
{
Debug.Log(message);
}
}
public static void w(string message)
{
if (W)
{
Debug.LogWarning(message);
}
}
public static void e(string message)
{
if (E)
{
Debug.LogError(message);
}
}
/// <summary>
/// Pause Game
/// </summary>
public static void b()
{
if (B)
{
Debug.Break();
}
}
/// <summary>
/// 按条件断言Debug
/// </summary>
/// <param name="condition">条件</param>
/// <param name="message">Log内容</param>
public static void assert(bool condition, string message)
{
if (A)
{
Debug.Assert(condition, message);
}
}
/// <summary>
/// 开启
/// </summary>
public static void OpenAll()
{
D = true;
W = true;
E = true;
A = true;
B = true;
}
public static void CloseAll()
{
D = false;
W = false;
E = false;
A = true;
B = true;
}
}
}