特点
显示时间,精确到毫秒 显示当前帧数(在主线程中的打印才有意义,非主线程显示为-1) 有三种条件编译符(如下图) 注: 要能显示线程中的当前帧数,要在app启动时,初始化mainThreadID字段 条件编译符的好处是,不需要的要打印的log调用不会产生任何额外的消耗。 如需要打印输出到文件,请自行扩展,写文件时建议用子线程执行
上代码
using System ;
using System. Diagnostics ;
using System. Threading ;
using UnityEngine ;
using Debug = UnityEngine. Debug ;
using Object = UnityEngine. Object ; namespace Main
{ public class XDebug { public static int mainThreadID { get ; set ; } = - 1 ; static bool _enable = true ; public static bool Enable { get { return _enable; } set { if ( value != _enable) { Debug. unityLogger. logEnabled = value ; _enable = value ; } } } [ Conditional ( "DEBUG_LOG_WARM_ERROR" ) ] public static void Log ( object message, Object context= null ) { if ( ! _enable) return ; string log = string . Concat ( GetPrefix ( ) , message) ; Debug. Log ( log, context) ; } [ Conditional ( "DEBUG_LOG_WARM_ERROR" ) ] public static void LogFormat ( Object context, string format, params object [ ] args) { if ( ! _enable) return ; string log = string . Concat ( GetPrefix ( ) , format) ; Debug. LogFormat ( context, log, args) ; } [ Conditional ( "DEBUG_LOG_WARM_ERROR" ) ] [ Conditional ( "DEBUG_WARM_ERROR" ) ] public static void LogWarning ( object message, Object context= null ) { if ( ! _enable) return ; string log = string . Concat ( GetPrefix ( ) , message) ; Debug. LogWarning ( log, context) ; } [ Conditional ( "DEBUG_LOG_WARM_ERROR" ) ] [ Conditional ( "DEBUG_WARM_ERROR" ) ] public static void LogWarningFormat ( Object context, string format, params object [ ] args) { if ( ! _enable) return ; string log = string . Concat ( GetPrefix ( ) , format) ; Debug. LogWarningFormat ( context, log, args) ; } [ Conditional ( "DEBUG_LOG_WARM_ERROR" ) ] [ Conditional ( "DEBUG_WARM_ERROR" ) ] [ Conditional ( "DEBUG_ERROR" ) ] public static void LogError ( object messag, Object context= null ) { if ( ! _enable) return ; string log = string . Concat ( GetPrefix ( ) , messag) ; Debug. LogError ( log, context) ; } [ Conditional ( "DEBUG_LOG_WARM_ERROR" ) ] [ Conditional ( "DEBUG_WARM_ERROR" ) ] [ Conditional ( "DEBUG_ERROR" ) ] public static void LogErrorFormat ( Object context, string format, params object [ ] args) { if ( ! _enable) return ; string log = string . Concat ( GetPrefix ( ) , format) ; Debug. LogErrorFormat ( context, log, args) ; } public static void Assert ( bool condition, object message, Object context= null ) { if ( ! _enable) return ; Debug. Assert ( condition, message, context) ; } public static void LogException ( Exception e, Object context= null ) { if ( ! _enable) return ; Debug. LogException ( e, context) ; } static string GetPrefix ( ) { int curFrame = - 1 ; if ( Thread. CurrentThread. ManagedThreadId == mainThreadID) { curFrame = Time. frameCount; } string curTime = DateTime. Now. ToString ( "HH:mm:ss.fff" ) ; return $"[ { curTime } ][ { curFrame } ] " ; } }
}
打个赏吧