Unity管理Debug.log輸出的Log

<p><span style="color: rgb(69, 69, 69); font-family: Verdana, 'Microsoft YaHei'; font-size: 16px; line-height: 30px;">在開發Unity的時候,在輸出log的時候大家都會採用Debug.log()的方式來輸出Log,但是遊戲發佈的時候這樣的Log是不能關閉的,在用戶手機運行產生一堆LOG也不是啥好事,一定要關閉才行、主要是unity官方也沒有提供直接關閉的方法。</span></p><p><span style="color: rgb(69, 69, 69); font-family: Verdana, 'Microsoft YaHei'; font-size: 16px; line-height: 30px;">參考文章:http://www.xuanyusong.com/archives/2782</span></p>
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);
            }
        }

        /// <summary>
        /// Debug 開關
        /// 默認爲開啓狀態true,false則不輸出Debug
        /// </summary>
        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


發佈了34 篇原創文章 · 獲贊 14 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章