(1)選擇在界面、console中輸出,而且可以設置保存到文檔函數
(2)控制debug是否輸出,能夠在debug模式下輸出,release模式下不輸出ui
謝謝雨鬆同窗的博客:http://www.xuanyusong.com/archives/2782 , 雨鬆飛天般的想法實在太奇妙了,雖然我如今尚未理解裏面的原理。雨鬆把debug類經常使用函數封裝在debuger類中,而後經過封裝DLL的方式解決Log輸出後的定位的問題。spa
Debug輸出控制類Debuger,經過EnableLog 控制書否輸出Log。debug
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錯誤。在VS模式下,調試
(1)能夠建議一個C# dll工程,.Net版本設置爲3.5(4.0也會提示不兼容,若是MonoDevelop生成,則提示的.Net版本更低)code
(2)引入unityEngine.dll庫,否則你編譯不過,OK ,而後編譯一下就行了。orm
爲了實現是否輸出、輸出位置、輸出參數控制以及在界面中不一樣輸出類型的效果、重複代碼的檢測等等,我頂層有從新進行封裝了一下,不過文件的save,沒仔細實現,直接代碼吧:blog
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; //打開界面調試信息輸出 public bool m_isEnableShowLogInGui = true; //打開調試堆棧信息輸出 public bool m_isEnableShowTraceInfoInGui = false ; //打開調試信息文件記錄 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/1jGBUK3Gget
轉載請註明:細雨淅淅