為了更好的識別打印信息,這里封裝了一下打印信息的工具類,雖然Unity中已經很好的識別..但是自己還是想實現新的工具類
DebugBase腳本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DebugBase<T> where T :new()
{
/// <summary>
/// 泛型單例
/// </summary>
static T instance;
public static T Instance {
get {
if (instance == null) {
instance = new T ();
}
return instance;
}
}
/// <summary>
/// 普通打印信息
/// </summary>
/// <param name="msg">Message.</param>
public virtual void Log (string msg)
{
if (!string.IsNullOrEmpty (msg)) {
Debug.Log (msg);
}
}
/// <summary>
/// 警告打印
/// </summary>
/// <param name="msg">Message.</param>
public virtual void LogWarning (string msg)
{
if (!string.IsNullOrEmpty (msg)) {
Debug.LogWarning (msg);
}
}
/// <summary>
/// 錯誤打印
/// </summary>
/// <param name="msg">Message.</param>
public virtual void LogError (string msg)
{
if (!string.IsNullOrEmpty (msg)) {
Debug.LogError (msg);
}
}
}
public class GameLog :DebugBase<GameLog>
{
/// <summary>
/// 重寫父類Log
/// </summary>
/// <param name="msg">Message.</param>
public override void Log (string msg)
{
if (!string.IsNullOrEmpty (msg)) {
base.Log ("*LOG*<color=white>" + msg + "</color>");
}
}
/// <summary>
/// 重寫父類LogWarning
/// </summary>
/// <param name="msg">Message.</param>
public override void LogWarning (string msg)
{
if (!string.IsNullOrEmpty (msg)) {
base.LogWarning ("*Warning*<color=yellow>" + msg + "</color>");
}
}
/// <summary>
/// 重寫父類LogError
/// </summary>
/// <param name="msg">Message.</param>
public override void LogError (string msg)
{
if (!string.IsNullOrEmpty (msg)) {
base.LogError ("*Error*<color=red>" + msg + "</color>");
}
}
}
Test腳本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Test : MonoBehaviour
{
Button btn;
// Use this for initialization
void Start ()
{
btn = transform.Find ("Button").GetComponent <Button> ();
btn.onClick.AddListener (delegate() {
GameLog.Instance.Log ("這是一個LOG");
GameLog.Instance.LogWarning ("這是一個LogWarning");
GameLog.Instance.LogError ("這是一個LogError");
});
}
}
效果如下:

