unity中Debug输出控制

1 需求:

(1)选择在界面、console中输出,并且能够设置保存到文档

(2)控制debug是否输出,可以在debug模式下输出,release模式下不输出

2 参考:

谢谢雨松同学的博客:http://www.xuanyusong.com/archives/2782 , 雨松飞天般的想法实在太奇妙了,虽然我现在还没有理解里面的原理。雨松把debug类常用函数封装在debuger类中,然后通过封装DLL的方式解决Log输出后的定位的问题。

3 方案:

  3.1  Dll生成

Debug输出控制类Debuger,通过EnableLog 控制书否输出Log。

using UnityEngine;
using System.Collections;

public class Debuger
{
    static public bool EnableLog = false;
    static public void Log(object message)
    {
        Log(message, null);
    }
    static public void Log(object message, Object context)
    {
        if (EnableLog)
        {
            Debug.Log(message, context);
        }
    }
    static public void LogError(object message)
    {
        LogError(message, null);
    }
    static public void LogError(object message, Object context)
    {
        if (EnableLog)
        {
            Debug.LogError(message, context);
        }
    }
    static public void LogWarning(object message)
    {
        LogWarning(message, null);
    }
    static public void LogWarning(object message, Object context)
    {
        if (EnableLog)
        {
            Debug.LogWarning(message, context);
        }
    }
}

 雨松提供下载的Dll,使用MonoDevelop编译器生成,在VS下可以用,但是会提示Debuger错误。在VS模式下,

(1)可以建议一个C# dll工程,.Net版本设置为3.5(4.0也会提示不兼容,如果MonoDevelop生成,则提示的.Net版本更低)

image

(2)引入unityEngine.dll库,不然你编译不过,OK ,然后编译一下就好了。

image

3.2 Unity中的使用

为了实现是否输出、输出位置、输出参数控制以及在界面中不同输出类型的效果、重复代码的检测等等,我顶层有重新进行封装了一下,不过文件的save,没仔细实现,直接代码吧:

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

public class strLog
{
    public string log;
    public LogType type;
    public uint num;

    public strLog(string _str, LogType _type, uint _num)
    {
        log = _str;
        type = _type;
        num = _num;
    }

    public void AddNum(uint _num)
    {
        Debug.Log(num);
        num += _num;
    }
}


public class DebuggerManager : MonoBehaviour
{
    //打开Debug信息输出
    public bool m_isEnableDebugOut = true;

    //打开界面调试信息输出
    public bool m_isEnableShowLogInGui = true;

    //打开调试堆栈信息输出
    public bool m_isEnableShowTraceInfoInGui = false ;

    //打开调试信息文件记录
    public bool m_isEnableSaveInText = false;

    void Start()
    {
        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);
        }
    }

    void Update()
    {
        WriteLogToFile();
    }

    void HandleLog(string logString, string stackTrace, LogType type)
    {
        if (m_isEnableShowLogInGui)
        {
            AddToShowList(type,logString);
        }

        if (m_isEnableShowTraceInfoInGui)
        {
            AddToShowList(type,stackTrace);
        }

        if (m_isEnableSaveInText)
        {
            AddToSaveList(logString);
        }
    }

    void OnGUI()
    {
        if (!m_isEnableShowLogInGui && !m_isEnableShowTraceInfoInGui)
            return;

        foreach(strLog log in m_GuiTextLines)
        {
            Color showClr = new Color(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 = 12;
            GUI.skin.label.alignment = TextAnchor.UpperLeft;
            GUILayout.Label("" + log.num.ToString() + "】 -->" + log.log);
        }
    }

   public  static void AddToShowList(LogType type , params object[] objs)
    {
        if (!Application.isPlaying)
        {
            return;
        }

        string strShowInGui = " ";

        for (int i = 0; i < objs.Length; ++i)
        {
            if (i == 0)
            {
                strShowInGui += objs[i].ToString();
            }
            else
            {
                strShowInGui += ", " + objs[i].ToString();
            }
        }

       for (int i = 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(new strLog(strShowInGui,type,0));
    }

    void WriteLogToFile()
    {
       if (false == m_isEnableDebugOut)
       {
           return;
       }

       if (m_TxtSavetoFile.Count > 0)
       {
           string[] temp = m_TxtSavetoFile.ToArray();
           foreach (string t in temp)
           {
               using (StreamWriter writer = new StreamWriter(m_DebugTextPath, true, Encoding.UTF8))
               {
                   writer.WriteLine(t);
               }
               m_TxtSavetoFile.Remove(t);
           }
       }
    }

    public static void AddToSaveList(string strLog)
    {
        m_TxtSavetoFile.Add(strLog);
    }


    static List<strLog> m_GuiTextLines = new List<strLog>();
    static List<string> m_TxtSavetoFile   = new List<string>();
    private string m_DebugTextPath;
    private const int constMaxNum_ShowInGui = 20;
}

4 效果

image

Demo下载:http://pan.baidu.com/s/1jGBUK3G

转载请注明:细雨淅淅

原文地址:https://www.cnblogs.com/zsb517/p/4038319.html