Unity Log管理(二) 绘制Log界面

https://gameinstitute.qq.com/community/detail/119540
因为是在game视图 所以editor脚本不管用
可以用windows小黑窗 或者直接在游戏显示
我选择在游戏显示 当不需要的时候就跟之前一样吧这个方法用特性直接干掉

/**
 *Copyright(C) 2019 by #COMPANY#
 *All rights reserved.
 *FileName:     #SCRIPTFULLNAME#
 *Author:       #AUTHOR#
 *Version:      #VERSION#
 *UnityVersion:#UNITYVERSION#
 *Date:         #DATE#
 *Description:   
 *History:
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DebugWnd : MonoBehaviour
{
    struct Log
    {
        public string msg;
        public string stackTrace;
        public LogType type;
    }

   
    public float openSpeed = 3f;

    readonly List<Log> logs = new List<Log>();
    Vector2 scrollPos;
    bool isVisble=true;
    bool collapse;

    static readonly Dictionary<LogType, Color> logTypeColors = new Dictionary<LogType, Color> {
            {LogType.Assert, Color.white },
            { LogType.Error, Color.red },
            { LogType.Exception, Color.red },
            { LogType.Log, Color.white },
            { LogType.Warning, Color.yellow },
    };

    //set wnd component
    const string wndTitle = "Console";
    const int margin = 20;
    static readonly GUIContent clearLable = new GUIContent("Clear", "Clear the contents of the console.");
    static readonly GUIContent collapseLable = new GUIContent("Collapse", "Hide repeated messages.");

    readonly Rect titleBarRect = new Rect(0, 0, 10000, 20);
    Rect wndRect = new Rect(margin, margin, Screen.width - (margin * 2), Screen.height - (margin * 2));
    void OnGUI()
    {
        if (!isVisble)
            return;

        wndRect = GUILayout.Window(123456, wndRect, DrawConsoleWnd, wndTitle);
    }

    void DrawConsoleWnd(int wndID)
    {
        DrawLogsList();
        DrawToolBar();

        GUI.DragWindow(titleBarRect);
    }

    void DrawLogsList()
    {
        scrollPos = GUILayout.BeginScrollView(scrollPos);

        for (int i = 0; i < logs.Count; i++)
        {
            Log log = logs[i];
            if (collapse&&i>1)
            {
                var previosuMsg = logs[i - 1].msg;

                if (log.msg==previosuMsg)               
                    continue;
                
            }
            GUI.contentColor = logTypeColors[log.type];
            GUILayout.Label(log.msg);
        }
        GUILayout.EndScrollView();
        //确保在绘制其他组件之前重置GUI颜色
        GUI.contentColor = Color.white;
    }

    void DrawToolBar()
    {
        GUILayout.BeginHorizontal();
        if (GUILayout.Button(clearLable))
        {
            //logs.Clear();
        }

        collapse = GUILayout.Toggle(collapse,collapseLable, GUILayout.ExpandWidth(false));
        GUILayout.EndHorizontal();
    }
}
image.png
/**
 *Copyright(C) 2019 by #COMPANY#
 *All rights reserved.
 *FileName:     #SCRIPTFULLNAME#
 *Author:       #AUTHOR#
 *Version:      #VERSION#
 *UnityVersion:#UNITYVERSION#
 *Date:         #DATE#
 *Description:   
 *History:
*/
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DebugWnd : MonoBehaviour
{
    struct Log
    {
        public string msg;
        public string stackTrace;
        public LogType type;
    }

   
    public float openSpeed = 3f;
    public bool isRestricLogCount = false;
    public int maxLogs = 1000;

    readonly List<Log> logs = new List<Log>();
    Vector2 scrollPos;
    bool isVisble=true;
    bool collapse;

    static readonly Dictionary<LogType, Color> logTypeColors = new Dictionary<LogType, Color> {
            {LogType.Assert, Color.white },
            { LogType.Error, Color.red },
            { LogType.Exception, Color.red },
            { LogType.Log, Color.white },
            { LogType.Warning, Color.yellow },
    };

    //set wnd component
    const string wndTitle = "Console";
    const int margin = 20;
    static readonly GUIContent clearLable = new GUIContent("Clear", "Clear the contents of the console.");
    static readonly GUIContent collapseLable = new GUIContent("Collapse", "Hide repeated messages.");

    readonly Rect titleBarRect = new Rect(0, 0, 10000, 20);
    Rect wndRect = new Rect(margin, margin, Screen.width - (margin * 2), Screen.height - (margin * 2));

    private void OnEnable()
    {
        Application.logMessageReceived+=HandleLog;
    }

    private void OnDisable()
    {
        Application.logMessageReceived -= HandleLog;
    }

    private void HandleLog(string condition, string stackTrace, LogType type)
    {
        logs.Add(new Log {
            msg=condition,
            stackTrace=stackTrace,
            type=type,
        });

    }

    void TrimExcessLogs()
    {
        if (!isRestricLogCount)
            return;

        var amountToRemove = Mathf.Max(logs.Count-maxLogs,0);

        if (amountToRemove==0)
            return;

        logs.RemoveRange(0, amountToRemove);
    }

    void OnGUI()
    {
        if (!isVisble)
            return;

        wndRect = GUILayout.Window(123456, wndRect, DrawConsoleWnd, wndTitle);
    }

    void DrawConsoleWnd(int wndID)
    {
        DrawLogsList();
        DrawToolBar();

        GUI.DragWindow(titleBarRect);
    }

    void DrawLogsList()
    {
        scrollPos = GUILayout.BeginScrollView(scrollPos);

        for (int i = 0; i < logs.Count; i++)
        {
            Log log = logs[i];
            if (collapse&&i>1)
            {
                var previosuMsg = logs[i - 1].msg;

                if (log.msg==previosuMsg)               
                    continue;               
            }
            GUI.contentColor = logTypeColors[log.type];
            GUILayout.Label(log.msg);
            GUILayout.Label(log.stackTrace);
        }
        GUILayout.EndScrollView();
    }

    void DrawToolBar()
    {
        GUILayout.BeginHorizontal();
        if (GUILayout.Button(clearLable))
        {
            logs.Clear();
        }

        collapse = GUILayout.Toggle(collapse,collapseLable, GUILayout.ExpandWidth(false));
        GUILayout.EndHorizontal();
    }


}
image.png

然后是要把绘制类和调动类分离 其实log类自带分离过了
还有的是滑动条要一直在最下方 虽然可以拖动 还有大小有点大 加上点击四个角能变大缩小
先做窗口放大缩小
我是尝试过手动控制但是没有获取左下角的api

这个是控制GUI滚动的
https://www.iteye.com/blog/convolute-1859851
我让进度条和log个数成一个正比关系 结果追不上 没办法走到最大值
而且这个进度条没有百分比 可能UGUI做好一点 只能手动翻了

image.png

然后batch上天了
image.png

/**
 *Copyright(C) 2019 by #COMPANY#
 *All rights reserved.
 *FileName:     #SCRIPTFULLNAME#
 *Author:       #AUTHOR#
 *Version:      #VERSION#
 *UnityVersion:#UNITYVERSION#
 *Date:         #DATE#
 *Description:   
 *History:
*/
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DebugWnd : MonoBehaviour
{
    struct Log
    {
        public string msg;
        public string stackTrace;
        public LogType type;
    }


    public float openSpeed = 3f;
    public bool isRestricLogCount = false;
    public int maxLogs = 1000;

    readonly List<Log> logs = new List<Log>();
    Vector2 scrollPos;
    bool isVisble = true;
    bool collapse;

    static readonly Dictionary<LogType, Color> logTypeColors = new Dictionary<LogType, Color> {
            {LogType.Assert, Color.white },
            { LogType.Error, Color.red },
            { LogType.Exception, Color.red },
            { LogType.Log, Color.white },
            { LogType.Warning, Color.yellow },
    };

    //set wnd component
    const string wndTitle = "Console";
    public float margin = 2;
    static readonly GUIContent clearLable = new GUIContent("Clear", "Clear the contents of the console.");
    static readonly GUIContent collapseLable = new GUIContent("Collapse", "Hide repeated messages.");

    Rect titleBarRect = new Rect(0, 0, 1000, 20);
    Rect wndRect;

    string marginText;

    private void OnEnable()
    {
        wndRect = new Rect(margin, margin, Screen.width / margin, Screen.height / margin);
        Application.logMessageReceived += HandleLog;
    }

    private void OnDisable()
    {
        Application.logMessageReceived -= HandleLog;
    }

    private void HandleLog(string condition, string stackTrace, LogType type)
    {
        logs.Add(new Log
        {
            msg = condition,
            stackTrace = stackTrace,
            type = type,
        });
        TrimExcessLogs();
    }

    void TrimExcessLogs()
    {
        if (!isRestricLogCount)
            return;

        var amountToRemove = Mathf.Max(logs.Count - maxLogs, 0);

        if (amountToRemove == 0)
            return;

        logs.RemoveRange(0, amountToRemove);
    }

    void OnGUI()
    {
        if (!isVisble)
            return;

        wndRect = GUILayout.Window(123456, wndRect, DrawConsoleWnd, wndTitle);

    }

    void DrawConsoleWnd(int wndID)
    {
        DrawLogsList();
        DrawToolBar();

        GUI.DragWindow(titleBarRect);
    }

    void DrawLogsList()
    {
        scrollPos = GUILayout.BeginScrollView(scrollPos);
        for (int i = 0; i < logs.Count; i++)
        {
            Log log = logs[i];
            if (collapse && i > 1)
            {
                var previosuMsg = logs[i - 1].msg;

                if (log.msg == previosuMsg)
                    continue;
            }
            GUI.contentColor = logTypeColors[log.type];
            GUILayout.Label($"{log.msg}\n\r{log.stackTrace}");
        }
        GUILayout.EndScrollView();
    }

    void DrawToolBar()
    {
        GUILayout.BeginHorizontal();
        if (GUILayout.Button(clearLable))
        {
            logs.Clear();
        }
        collapse = GUILayout.Toggle(collapse, collapseLable, GUILayout.ExpandWidth(false));
        GUILayout.Label("zoom", GUILayout.Width(40), GUILayout.Height(40));
        marginText = GUILayout.TextField(marginText, GUILayout.Width(30), GUILayout.Height(30));

        if (marginText != null && marginText != "" && marginText != "0")
            margin = float.Parse(marginText);
        wndRect = new Rect(margin, margin, Screen.width / margin, Screen.height / margin);
        GUILayout.EndHorizontal();
    }


}

然后有人告诉我有个插件
https://github.com/aliessmael/Unity-Logs-Viewer

image.png

batch稳定 应该是回收了多余空间
image.png

image.png

image.png

这个插件挺好用的
附上 源码和插件地址
https://github.com/1004019267/LogTools

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容