Unity调试:在GUI界面绘制Log信息

  • Post author:
  • Post category:其他


本文的主要目的是在将Unity项目发布成APK后,在APK的操作界面方便查看应用中的LOG信息,以方便调试

实现的方式主要是利用Unity自带的Debug.Log()方法再次封装,使用GUI的API将自己的LOG信息显示在上层界面

闲话不多说,直接上代码:

这个脚本主要是用来接收项目中打印的LOG信息

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

public class GameDebugLog : System.Object
{

    /// <summary>
    /// Log level .定义LOG的类型:正常、警告、错误、异常
    /// </summary>
    public enum LogLevel : byte
    {
        /// <summary>
        /// The normal.
        /// </summary>
        Normal = 0,
        /// <summary>
        /// The warning.
        /// </summary>
        Warning,
        /// <summary>
        /// The error.
        /// </summary>
        Error,
        /// <summary>
        /// The exception.
        /// </summary>
        Exception,
    }

    /// <summary>
    /// Log the specified msg and objs.
    /// </summary>
    /// <param name="msg">Message.</param>
    /// <param name="objs">Objects.</param>
    public static void Log(string msg, params object[] objs)
    {
        OutputLog(LogLevel.Normal, msg, objs);
    }

    /// <summary>
    /// Logs the warning.
    /// </summary>
    /// <param name="msg">Message.</param>
    /// <param name="objs">Objects.</param>
    public static void LogWarning(string msg, params object[] objs)
    {
        OutputLog(LogLevel.Warning, msg, objs);
    }

    /// <summary>
    /// Logs the error.
    /// </summary>
    /// <param name="msg">Message.</param>
    /// <param name="objs">Objects.</param>
    public static void LogError(string msg, params object[] objs)
    {
        OutputLog(LogLevel.Error, msg, objs);
    }

    /// <summary>
    /// Logs the exception.
    /// </summary>
    /// <param name="msg">Message.</param>
    /// <param name="objs">Objects.</param>
    public static void LogException(System.Exception e)
    {
        OutputExceptionLog(e);
    }

    /// <summary>
    /// Log content.
    /// </summary>
    public class LogContent
    {
        public string Msg { get; set; }
        public LogLevel Level { get; set; }
    }

    /// <summary>
    /// The logs.
    /// </summary>
    public static List<LogContent> logs;

    /// <summary>
    /// Adds the log centent.
    /// </summary>
    /// <param name="level">Level.</param>
    /// <param name="msg">Message.</param>
    public static void AddLogCentent(LogLevel level, string msg)
    {
        if (logs == null) logs = new List<LogContent>();
        if (logs != null)
        {
            if (logs.Count > 20) logs.RemoveAt(0);
            LogContent log = new LogContent();
            log.Msg = msg;
            log.Level = level;
            logs.Add(log);
        }
    }

    public static bool isEditor = Application.isEditor;

    /// <summary>
    /// Outputs the log.
    /// </summary>
    /// <param name="level">Level.</param>
    /// <param name="msg">Message.</param>
    /// <param name="objs">Objects.</param>
    public static void OutputLog(LogLevel level, string msg, params object[] objs)
    {
        StringBuilder sb = new StringBuilder();
        sb.AppendFormat(msg, objs);

#if DEBUG_LOG_EDITOR
			if (Application.isPlaying && !isEditor)
#else
        if (Application.isPlaying)
#endif
        {
            AddLogCentent(level, sb.ToString());
        }
        else
        {
            if (level == LogLevel.Normal)
                Debug.Log(sb.ToString());
            else if (level == LogLevel.Warning)
                Debug.LogWarning(sb.ToString());
            else if (level == LogLevel.Error)
                Debug.LogError(sb.ToString());
        }
    }

    /// <summary>
    /// Outputs the exception log.
    /// </summary>
    /// <param name="e">E.</param>
    public static void OutputExceptionLog(System.Exception e)
    {
#if DEBUG_LOG_EDITOR
			if (Application.isPlaying && !isEditor)
#else
        if (Application.isPlaying)
#endif
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("{0} \n{1}", e.Message, e.StackTrace);

            AddLogCentent(LogLevel.Exception, sb.ToString());
        }
        else
        {
            Debug.LogError(e);
        }
        Debug.LogError(e);
    }

    /// <summary>
    /// The log position.
    /// </summary>
    public static Vector2 logPos;
    /// <summary>
    /// The log rect.
    /// </summary>
    public static Rect logRect;
    /// <summary>
    /// The level setting.
    /// </summary>
    public class LevelSetting
    {
        /// <summary>
        /// Gets or sets the title.
        /// </summary>
        /// <value>The title.</value>
        public string Title { get; set; }
        /// <summary>
        /// Gets or sets the color of the title.
        /// </summary>
        /// <value>The color of the title.</value>
        public Color TitleColor { get; set; }
    }
    /// <summary>
    /// Level settings.
    /// </summary>
    
}

这个方法主要是在你的主类中进行调用,使用的是Unity的生命周期方法

public void OnGUI()
    {
        if (!Application.isPlaying) return;

        if (levelSettings == null)
        {
            levelSettings = new Dictionary<GameDebugLog.LogLevel, GameDebugLog.LevelSetting>();

            levelSettings[GameDebugLog.LogLevel.Normal] = new GameDebugLog.LevelSetting();
            levelSettings[GameDebugLog.LogLevel.Normal].Title = "Normal: ";
            levelSettings[GameDebugLog.LogLevel.Normal].TitleColor = Color.blue;

            levelSettings[GameDebugLog.LogLevel.Warning] = new GameDebugLog.LevelSetting();
            levelSettings[GameDebugLog.LogLevel.Warning].Title = "Warning: ";
            levelSettings[GameDebugLog.LogLevel.Warning].TitleColor = new Color(1f, 0.5f, 0f, 1f);

            levelSettings[GameDebugLog.LogLevel.Error] = new GameDebugLog.LevelSetting();
            levelSettings[GameDebugLog.LogLevel.Error].Title = "Error: ";
            levelSettings[GameDebugLog.LogLevel.Error].TitleColor = new Color(1f, 0f, 0f, 1f);

            levelSettings[GameDebugLog.LogLevel.Exception] = new GameDebugLog.LevelSetting();
            levelSettings[GameDebugLog.LogLevel.Exception].Title = "Exception: ";
            levelSettings[GameDebugLog.LogLevel.Exception].TitleColor = new Color(1f, 0f, 0.5f, 1f);

            GameDebugLog.logPos = Vector2.zero;
            GameDebugLog.logRect = new Rect(Screen.width - 402, Screen.height - 268, 400, 266);
        }

        Color backgroundColor = GUI.backgroundColor;
        Color oldContentColor = GUI.contentColor;
        GUI.backgroundColor = new Color(0.5f, 0.0f, 0.0f, 0.5f);
        GUI.Box(GameDebugLog.logRect, "");
        GUILayout.BeginArea(GameDebugLog.logRect);
        GameDebugLog.logPos = GUILayout.BeginScrollView(GameDebugLog.logPos, GUILayout.Width(400), GUILayout.Height(266));

        if (GameDebugLog.logs != null)
        {
            for (int i = 0, imax = GameDebugLog.logs.Count; i < imax; ++i)
            {
                GUI.contentColor = levelSettings[GameDebugLog.logs[i].Level].TitleColor;
                //GUILayout.BeginHorizontal();
                GUILayout.Label(levelSettings[GameDebugLog.logs[i].Level].Title, GUILayout.Width(80));
                GUI.contentColor = Color.green;
                GUILayout.Label(GameDebugLog.logs[i].Msg);
                //GUILayout.EndHorizontal();
            }
        }

        GUILayout.EndScrollView();
        GUILayout.EndArea();
        GUI.contentColor = oldContentColor;
        GUI.backgroundColor = backgroundColor;
    }

显示效果如下图:


在项目中可以通过这样的方式方便定位问题所在,希望也能方便各位开发大大哦!



版权声明:本文为WandDouDou原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。