1 Requirements:
(1) Select the output in the interface, console, and can be set to save to the document
(2) Control debug output, can be output in debug mode, release mode does not output
2 References:
Thank you for the Rain Song Classmate's blog: http://www.xuanyusong.com/archives/2782, rain pine flying like the idea is too wonderful, although I still do not understand the principle of the inside. Rain pine encapsulates the common functions of the Debug class in the Debuger class, and then solves the problem of locating the log output by encapsulating the DLL.
3 Scenario: 3.1 DLL generation
Debug output Control class Debuger, via Enablelog control book no output log.
usingUnityengine;usingSystem.Collections; Public classdebuger{Static Public BOOLEnablelog =false; Static Public voidLog (Objectmessage) {Log (message,NULL); } Static Public voidLog (Objectmessage, Object context) { if(Enablelog) {Debug.Log (message, context); } } Static Public voidLogError (Objectmessage) {LogError (message,NULL); } Static Public voidLogError (Objectmessage, Object context) { if(Enablelog) {debug.logerror (message, context); } } Static Public voidLogwarning (Objectmessage) {logwarning (message,NULL); } Static Public voidLogwarning (Objectmessage, Object context) { if(Enablelog) {debug.logwarning (message, context); } }}
Rain Pine provides downloaded DLLs, generated using the MonoDevelop compiler, and can be used in VS, but will prompt for debuger errors. In VS Mode,
(1) A C # DLL project can be proposed. NET version is set to 3.5 (4.0 also prompts for incompatibility, if MonoDevelop is generated, the prompt. NET version is lower)
(2) Introduce the UnityEngine.dll library, otherwise you compile, OK, and then compile a bit better.
3.2 Use in Unity
In order to achieve output, output position, output parameter control and different output types in the interface effect, duplicate code detection and so on, I have to re-encapsulate the top layer, but the file save, not carefully implemented, direct code:
usingUnityengine;usingSystem.Collections;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Text; Public classstrlog{ Public stringlog; PublicLogType type; Public UINTnum; PublicStrlog (string_str, LogType _type,UINT_num) {Log=_str; Type=_type; Num=_num; } Public voidAddnum (UINT_num) {Debug.Log (num); Num+=_num; }} Public classdebuggermanager:monobehaviour{//turn on debug information output Public BOOLM_isenabledebugout =true; //Open Interface Debug information output Public BOOLM_isenableshowlogingui =true; //To open the debug stack information output Public BOOLM_isenableshowtraceinfoingui =false ; //Open Debug information file record Public BOOLM_isenablesaveintext =false; voidStart () {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); } } voidUpdate () {writelogtofile (); } voidHandlelog (stringLogstring,stringstackTrace, LogType type) { if(M_isenableshowlogingui) {addtoshowlist (type,logstring); } if(M_isenableshowtraceinfoingui) {addtoshowlist (type,stacktrace); } if(M_isenablesaveintext) {addtosavelist (logstring); } } voidOngui () {if(!m_isenableshowlogingui &&!)M_isenableshowtraceinfoingui)return; foreach(Strlog Loginchm_guitextlines) {Color SHOWCLR=NewColor (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= A; GUI.skin.label.alignment=Textanchor.upperleft; Guilayout.label ("""+ log.num.ToString () +"" -"+log.log); } } Public Static voidAddtoshowlist (LogType type,params Object[] objs) { if(!application.isplaying) {return; } stringStrshowingui =" "; for(inti =0; I < Objs. Length; ++i) {if(i = =0) {Strshowingui+=Objs[i]. ToString (); } Else{Strshowingui+=", "+Objs[i]. ToString (); } } for(inti =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 (NewStrlog (Strshowingui,type,0)); } voidWritelogtofile () {if(false==m_isenabledebugout) { return; } if(M_txtsavetofile.count >0) { string[] temp =M_txtsavetofile.toarray (); foreach(stringTinchtemp) { using(StreamWriter writer =NewStreamWriter (M_debugtextpath,true, Encoding.UTF8)) {writer. WriteLine (t); } m_txtsavetofile.remove (t); } } } Public Static voidAddtosavelist (stringStrlog) {M_txtsavetofile.add (Strlog); } StaticList<strlog> M_guitextlines =NewList<strlog>(); Staticlist<string> m_txtsavetofile =Newlist<string>(); Private stringM_debugtextpath; Private Const intConstmaxnum_showingui = -;}
4 effects
Demo Download: Http://pan.baidu.com/s/1jGBUK3G
Reprint Please specify: drizzle
Debug output control in Unity