Unity管理Debug.log输出的Log

在开发Unity的时候,在输出log的时候大家都会采用Debug.log()的方式来输出Log,但是游戏发布的时候这样的Log是不能关闭的,在用户手机运行产生一堆LOG也不是啥好事,一定要关闭才行、主要是unity官方也没有提供直接关闭的方法。

参考文章:http://www.xuanyusong.com/archives/2782

using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;

namespace TDebugger
{
    public class TDebug
    {
        // 摘要:
        //     Opens or closes developer console.
        public static bool developerConsoleVisible
        {
            get { return Debug.developerConsoleVisible; }
            set { Debug.developerConsoleVisible = value; }
        }

        //
        // 摘要:
        //     In the Build Settings dialog there is a check box called "Development Build".
        public static bool isDebugBuild
        {
            get { return Debug.isDebugBuild; }
        }

        public static void Break()
        {
            Debug.Break();
        }

        public static void ClearDeveloperConsole()
        {
            Debug.ClearDeveloperConsole();
        }

        public static void DebugBreak()
        {
            Debug.DebugBreak();
        }

        //
        // 摘要:
        //     Draws a line from the point start to end with color for a duration of time
        //     and with or without depth testing. If duration is 0 then the line is rendered
        //     1 frame.
        public static void DrawLine(Vector3 start, Vector3 end)
        {
            if (s_TDebugToggle)
            {
                Debug.DrawLine(start, end);
            }
        }

        //
        // 摘要:
        //     Draws a line from the point start to end with color for a duration of time
        //     and with or without depth testing. If duration is 0 then the line is rendered
        //     1 frame.
        public static void DrawLine(Vector3 start, Vector3 end, Color color)
        {
            if (s_TDebugToggle)
            {
                Debug.DrawLine(start, end, color);
            }
        }

        //
        // 摘要:
        //     Draws a line from the point start to end with color for a duration of time
        //     and with or without depth testing. If duration is 0 then the line is rendered
        //     1 frame.
        public static void DrawLine(Vector3 start, Vector3 end, Color color, float duration)
        {
            if (s_TDebugToggle)
            {
                Debug.DrawLine(start, end, color, duration);
            }
        }

        //
        // 摘要:
        //     Draws a line from the point start to end with color for a duration of time
        //     and with or without depth testing. If duration is 0 then the line is rendered
        //     1 frame.

        public static void DrawLine(Vector3 start, Vector3 end, Color color, float duration, bool depthTest)
        {
            if (s_TDebugToggle)
            {
                Debug.DrawLine(start, end, color, duration, depthTest);
            }
        }

        //
        // 摘要:
        //     Draws a line from start to start + dir with color for a duration of time
        //     and with or without depth testing. If duration is 0 then the line is rendered
        //     1 frame.
        public static void DrawRay(Vector3 start, Vector3 dir)
        {
            if (s_TDebugToggle)
            {
                Debug.DrawRay(start, dir);
            }
        }

        //
        // 摘要:
        //     Draws a line from start to start + dir with color for a duration of time
        //     and with or without depth testing. If duration is 0 then the line is rendered
        //     1 frame.
        public static void DrawRay(Vector3 start, Vector3 dir, Color color)
        {
            if (s_TDebugToggle)
            {
                Debug.DrawRay(start, dir, color);
            }
        }

        //
        // 摘要:
        //     Draws a line from start to start + dir with color for a duration of time
        //     and with or without depth testing. If duration is 0 then the line is rendered
        //     1 frame.
        public static void DrawRay(Vector3 start, Vector3 dir, Color color, float duration)
        {
            if (s_TDebugToggle)
            {
                Debug.DrawRay(start, dir, color, duration);
            }
        }

        //
        // 摘要:
        //     Draws a line from start to start + dir with color for a duration of time
        //     and with or without depth testing. If duration is 0 then the line is rendered
        //     1 frame.
        public static void DrawRay(Vector3 start, Vector3 dir, Color color, float duration, bool depthTest)
        {
            if (s_TDebugToggle)
            {
                Debug.DrawRay(start, dir, color, duration, depthTest);
            }
        }

        //
        // 摘要:
        //     Logs message to the Unity Console.
        public static void Log(object message)
        {
            if (s_TDebugToggle)
            {
                Debug.Log(message);
            }
        }

        //
        // 摘要:
        //     Logs message to the Unity Console.
        public static void Log(object message, UnityEngine.Object context)
        {
            if (s_TDebugToggle)
            {
                Debug.Log(message, context);
            }
        }

        //
        // 摘要:
        //     A variant of Debug.Log that logs an error message to the console.
        public static void LogError(object message)
        {
            if (s_TDebugToggle)
            {
                Debug.LogError(message);
            }
        }

        //
        // 摘要:
        //     A variant of Debug.Log that logs an error message to the console.
        public static void LogError(object message, UnityEngine.Object context)
        {
            if (s_TDebugToggle)
            {
                Debug.LogError(message, context);
            }
        }

        //
        // 摘要:
        //     A variant of Debug.Log that logs an error message from an exception to the
        //     console.
        public static void LogException(Exception exception)
        {
            if (s_TDebugToggle)
            {
                Debug.LogException(exception);
            }
        }

        //
        // 摘要:
        //     A variant of Debug.Log that logs an error message from an exception to the
        //     console.
        public static void LogException(Exception exception, UnityEngine.Object context)
        {
            if (s_TDebugToggle)
            {
                Debug.LogException(exception, context);
            }
        }

        //
        // 摘要:
        //     A variant of Debug.Log that logs a warning message to the console.
        public static void LogWarning(object message)
        {
            if (s_TDebugToggle)
            {
                Debug.LogWarning(message);
            }
        }

        //
        // 摘要:
        //     A variant of Debug.Log that logs a warning message to the console.
        public static void LogWarning(object message, UnityEngine.Object context)
        {
            if (s_TDebugToggle)
            {
                Debug.LogWarning(message, context);
            }
        }

        /// 
        /// Debug 开关
        /// 默认为开启状态true,false则不输出Debug
        /// 
        public static bool Toggle
        {
            get { return s_TDebugToggle; }
            set { s_TDebugToggle = value; }
        }

        private static bool s_TDebugToggle = true;
    }
}


将上面的代码打包成DLL,在Unity项目中测试证明可行,下面分享下这个测试包:http://pan.baidu.com/s/1dDq9qpF


你可能感兴趣的:(Unity)