1 需求:(1)選擇在界面、console中輸出,并且能夠設(shè)置保存到文檔 (2)控制debug是否輸出,可以在debug模式下輸出,release模式下不輸出 2 參考:謝謝雨松同學(xué)的博客: http://www./archives/2782 , 雨松飛天般的想法實(shí)在太奇妙了,雖然我現(xiàn)在還沒有理解里面的原理。雨松把debug類常用函數(shù)封裝在debuger類中,然后通過封裝DLL的方式解決Log輸出后的定位的問題。 3 方案:3.1 Dll生成Debug輸出控制類Debuger,通過EnableLog 控制書否輸出Log。 using UnityEngine;
using System.Collections;
public class Debuger
{
static public bool EnableLog = false;
static public void Log(object message)
{
Log(message, null);
}
static public void Log(object message, Object context)
{
if (EnableLog)
{
Debug.Log(message, context);
}
}
static public void LogError(object message)
{
LogError(message, null);
}
static public void LogError(object message, Object context)
{
if (EnableLog)
{
Debug.LogError(message, context);
}
}
static public void LogWarning(object message)
{
LogWarning(message, null);
}
static public void LogWarning(object message, Object context)
{
if (EnableLog)
{
Debug.LogWarning(message, context);
}
}
} 雨松提供下載的Dll,使用MonoDevelop編譯器生成,在VS下可以用,但是會提示Debuger錯(cuò)誤。在VS模式下, (1)可以建議一個(gè)C# dll工程,.Net版本設(shè)置為3.5(4.0也會提示不兼容,如果MonoDevelop生成,則提示的.Net版本更低) (2)引入unityEngine.dll庫,不然你編譯不過,OK ,然后編譯一下就好了。 3.2 Unity中的使用為了實(shí)現(xiàn)是否輸出、輸出位置、輸出參數(shù)控制以及在界面中不同輸出類型的效果、重復(fù)代碼的檢測等等,我頂層有重新進(jìn)行封裝了一下,不過文件的save,沒仔細(xì)實(shí)現(xiàn),直接代碼吧: using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
public class strLog
{
public string log;
public LogType type;
public uint num;
public strLog(string _str, LogType _type, uint _num)
{
log = _str;
type = _type;
num = _num;
}
public void AddNum(uint _num)
{
Debug.Log(num);
num += _num;
}
}
public class DebuggerManager : MonoBehaviour
{
//打開Debug信息輸出
public bool m_isEnableDebugOut = true;
//打開界面調(diào)試信息輸出
public bool m_isEnableShowLogInGui = true;
//打開調(diào)試堆棧信息輸出
public bool m_isEnableShowTraceInfoInGui = false ;
//打開調(diào)試信息文件記錄
public bool m_isEnableSaveInText = false;
void Start()
{
if (false == m_isEnableDebugOut)
{
Debuger.EnableLog = false;
return;
}
Debuger.EnableLog = true;
Application.RegisterLogCallback(HandleLog);
if (false == m_isEnableSaveInText)
return;
m_DebugTextPath = Application.persistentDataPath + "/outLog.txt";
if (System.IO.File.Exists(m_DebugTextPath))
{
File.Delete(m_DebugTextPath);
}
}
void Update()
{
WriteLogToFile();
}
void HandleLog(string logString, string stackTrace, LogType type)
{
if (m_isEnableShowLogInGui)
{
AddToShowList(type,logString);
}
if (m_isEnableShowTraceInfoInGui)
{
AddToShowList(type,stackTrace);
}
if (m_isEnableSaveInText)
{
AddToSaveList(logString);
}
}
void OnGUI()
{
if (!m_isEnableShowLogInGui && !m_isEnableShowTraceInfoInGui)
return;
foreach(strLog log in m_GuiTextLines)
{
Color showClr = new Color(0.0f, 0.0f, 0.0f, 1.0f);
if (log.type == LogType.Error)
{
showClr.r = 1.0f;
}
else if (log.type == LogType.Warning)
{
showClr.r = 1.0f;
showClr.g = 1.0f;
}
else if (log.type == LogType.Log)
{
showClr.g = 1.0f;
}
GUI.skin.label.normal.textColor = showClr;
GUI.skin.label.fontSize = 12;
GUI.skin.label.alignment = TextAnchor.UpperLeft;
GUILayout.Label("【" + log.num.ToString() + "】 -->" + log.log);
}
}
public static void AddToShowList(LogType type , params object[] objs)
{
if (!Application.isPlaying)
{
return;
}
string strShowInGui = " ";
for (int i = 0; i < objs.Length; ++i)
{
if (i == 0)
{
strShowInGui += objs[i].ToString();
}
else
{
strShowInGui += ", " + objs[i].ToString();
}
}
for (int i = 0; i < m_GuiTextLines.Count; ++i)
{
if (m_GuiTextLines[i].log == strShowInGui)
{
m_GuiTextLines[i].AddNum(1);
return;
}
}
if (m_GuiTextLines.Count > constMaxNum_ShowInGui)
{
m_GuiTextLines.RemoveAt(0);
}
m_GuiTextLines.Add(new strLog(strShowInGui,type,0));
}
void WriteLogToFile()
{
if (false == m_isEnableDebugOut)
{
return;
}
if (m_TxtSavetoFile.Count > 0)
{
string[] temp = m_TxtSavetoFile.ToArray();
foreach (string t in temp)
{
using (StreamWriter writer = new StreamWriter(m_DebugTextPath, true, Encoding.UTF8))
{
writer.WriteLine(t);
}
m_TxtSavetoFile.Remove(t);
}
}
}
public static void AddToSaveList(string strLog)
{
m_TxtSavetoFile.Add(strLog);
}
static List<strLog> m_GuiTextLines = new List<strLog>();
static List<string> m_TxtSavetoFile = new List<string>();
private string m_DebugTextPath;
private const int constMaxNum_ShowInGui = 20;
} 4 效果 Demo下載: http://pan.baidu.com/s/1jGBUK3G 轉(zhuǎn)載請注明:細(xì)雨淅淅 |
|