Unity,将控制台输出的warning、error、log显示到游戏界面中,以便打包出来测试使用

前几天项目要上线,打包出来的放到安卓手机上出现了bug,但是在编辑器里面却没有报错,所以我们主程就写出来了这一个功能来调试bug,我觉得很好用,所以拿出来分享一下

主要就是一个回调函数 Application.logMessageReceived

    Application.logMessageReceived += HandleLog;
    gameObject.AddComponent();
    
    public string output = "";
    public string stack = "";
    void HandleLog(string logString, string stackTrace, LogType type)
    {
        output = logString;
        stack = stackTrace;
        if (type == LogType.Error)
        {
            output = "" + output + "";
            stack = "" + stack + "";
        }
        else if (type == LogType.Warning)
        {
            output = "" + output + "";
            stack = "" + stack + "";
        }
        else
        {
            output = "" + output + "";
            stack = "" + stack + "";
        }
        //ShowDebug.Add(output, stack);
        MyDebug.Add(output, stack);
    }
public class MyDebug : MonoBehaviour
{
    private Vector2 ScrollPos;
    public static List messages = new List();
    public static List names = new List();
    public static bool isShow = false;

    void OnGUI()
    {
        if (!isShow) return;
        //gUIStyle.stretchWidth = 20;
        ScrollPos = GUI.BeginScrollView(new Rect(0, 30, 600 * (Screen.width / 720), Screen.height),
            ScrollPos, new Rect(0, 0, 100000000, 100000000));

        //ScrollPos = GUI.BeginScrollView(new Rect(10, 10, 400, 400), ScrollPos, new Rect(10, 10, 770, 600));

        float posY = 0;
        for (int i = 0; i < names.Count; i++)
        {
            GUIContent tempContent = new GUIContent();
            tempContent.text = names[i] + " : " + messages[i];
            GUIStyle bb = new GUIStyle();
            bb.fixedWidth = 600 * (Screen.width / 720);
            bb.wordWrap = true;
            bb.fontSize = 40 * (Screen.width / 720);
            float H = bb.CalcHeight(tempContent, 600 * (Screen.width / 720));
            GUI.Label(new Rect(0, posY, 600 * (Screen.width / 720), H), tempContent, bb);
            posY += H;
            //GUILayout.Space(10);
        }

        GUI.EndScrollView();

    }

    public static void Add(string name, string message)
    {
        if (names.Contains(name) == false)
        {
            names.Add(name);
            messages.Add(message);
        }
        else
        {
            for (int i = 0; i < names.Count; i++)
            {
                if (names[i] == name)
                {
                    messages[i] = message;
                    break;
                }
            }
        }
    }
}

Unity,将控制台输出的warning、error、log显示到游戏界面中,以便打包出来测试使用_第1张图片

Game视图下的GUI输出

 

---------------------------------2.0版本-----------------------------------------

之前还不怎么会编辑器,最近学了一下,改进了一下界面

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

public class DebuggerLog : MonoBehaviour {


    public struct LogInfo
    {
        public LogType type;
        public string desc;

        public LogInfo(LogType type, string desc)
        {
            this.type = type;
            this.desc = desc;
        }
    }

    //错误详情
    public List m_logEntries = new List();
    public List m_logLog = new List();
    public List m_logWarning = new List();
    public List m_logError = new List();
    public List curLog = new List();
    //是否显示错误窗口
    private bool m_IsVisible = false;
    //窗口显示区域
    private Rect m_WindowRect = new Rect(0, 0, Screen.width, Screen.height);
    //窗口滚动区域
    private Vector2 m_scrollPositionText = Vector2.zero;
    //字体大小
    private int fontSize = 30;

    public List s1 = new List();

    GUISkin skin;

    private void Start()
    {
        skin = Resources.Load("GUISkin");
        curLog = m_logEntries;
        监听错误
        Application.logMessageReceivedThreaded += (condition, stackTrace, type) =>
        {
            if (!m_IsVisible)
            {
                m_IsVisible = true;
            }
            switch (type)
            {
                case LogType.Warning:
                    m_logWarning.Add(new LogInfo(type, string.Format("{0}\n{1}", condition, stackTrace)));
                    break;
                case LogType.Log:
                    m_logLog.Add(new LogInfo(type, string.Format("{0}\n{1}", condition, stackTrace)));
                    break;
                case LogType.Error:
                case LogType.Exception:
                    m_logError.Add(new LogInfo(type, string.Format("{0}\n{1}", condition, stackTrace)));
                    break;
            }
            m_logEntries.Add(new LogInfo(type, string.Format("{0}\n{1}", condition, stackTrace)));
            s1.Add(stackTrace);
        };

        for (int i = 0; i < 10; i++)
        {
            Debug.LogError("错误啊!!");
            Debug.LogWarning("警告啊!!");
            print("正常输出");
        }
        int[] a = null;
        a[1] = 100;
    }

    void OnGUI()
    {
        if (m_IsVisible)
        {
            m_WindowRect = GUILayout.Window(0, m_WindowRect, ConsoleWindow, "Console");
        }
    }

    //日志窗口
    void ConsoleWindow(int windowID)
    {
        GUILayout.BeginHorizontal();
        skin.button.fontSize = fontSize;
        skin.textArea.fontSize = fontSize;
        if (GUILayout.Button("Clear", skin.button, GUILayout.MaxWidth(200),GUILayout.MaxHeight(100)))
        {
            m_logEntries.Clear();
        }
        if (GUILayout.Button("Close", skin.button, GUILayout.MaxWidth(200), GUILayout.MaxHeight(100)))
        {
            m_IsVisible = false;
        }
        if (GUILayout.Button("AddFontSize", skin.button, GUILayout.MaxWidth(200), GUILayout.MaxHeight(100)))
        {
            fontSize++;
        }
        if (GUILayout.Button("ReduceFontSize", skin.button, GUILayout.MaxWidth(200), GUILayout.MaxHeight(100)))
        {
            fontSize--;
        }
        if (GUILayout.Button("Log", skin.button, GUILayout.MaxWidth(200), GUILayout.MaxHeight(100)))
        {
            if (curLog == m_logLog)
                curLog = m_logEntries;
            else
                curLog = m_logLog;
        }
        if (GUILayout.Button("Warning", skin.button, GUILayout.MaxWidth(200), GUILayout.MaxHeight(100)))
        {
            if (curLog == m_logWarning)
                curLog = m_logEntries;
            else
                curLog = m_logWarning;
        }
        if (GUILayout.Button("Error", skin.button, GUILayout.MaxWidth(200), GUILayout.MaxHeight(100)))
        {
            if (curLog == m_logError)
                curLog = m_logEntries;
            else
                curLog = m_logError;
        }
        GUILayout.EndHorizontal();
        m_scrollPositionText = GUILayout.BeginScrollView(m_scrollPositionText,skin.horizontalScrollbar,skin.verticalScrollbar);
        foreach (var entry in curLog)
        {
            Color currentColor = GUI.contentColor;
            switch (entry.type)
            {
                case LogType.Warning:
                    GUI.contentColor = Color.white;
                    break;
                case LogType.Assert:
                    GUI.contentColor = Color.black;
                    break;
                case LogType.Log:
                    GUI.contentColor = Color.green;
                    break;
                case LogType.Error:
                case LogType.Exception:
                    GUI.contentColor = Color.red;
                    break;
            }
            GUILayout.Label(entry.desc, skin.textArea);
            GUI.contentColor = currentColor;
        }
        GUILayout.EndScrollView();
    }

}

Unity,将控制台输出的warning、error、log显示到游戏界面中,以便打包出来测试使用_第2张图片结果图

你可能感兴趣的:(Unity3D)