实用Unity3D Log打印工具XDebug

特点

  • 显示时间,精确到毫秒
  • 显示当前帧数(在主线程中的打印才有意义,非主线程显示为-1)
  • 有三种条件编译符(如下图)
    实用Unity3D Log打印工具XDebug_第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
    {
        /// 
        /// 主线程id
        /// 
        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}] ";
        }
    }
}

打个赏吧

实用Unity3D Log打印工具XDebug_第2张图片

你可能感兴趣的:(Unity开发实用干货,unity3d,debug)