运行时Scene显示Debug框打印信息

论坛 期权论坛 脚本     
匿名网站用户   2020-12-20 23:51   31   0
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class LogObject
{
    public string output = "";
    public string stack = "";
    public LogType logType;
}

public class DebugOutside : MonoBehaviour
{
    //public long maxLogCount = 200;

    private List<LogObject> lo = new List<LogObject>();
    private Rect m_logWinRect = new Rect(0, 0, 600, 400);
    private Rect m_minBtnRect = new Rect(0, 0, 140, 55);
    private Vector2 scrollPosition;
    private bool m_showLog = true;
    private bool m_showWarning = true;
    private bool m_showError = true;
    private bool m_showLogWin = true;

    void Start()
    {
    }

    void OnEnable()
    {
        Application.RegisterLogCallback(HandleLog);
    }
    void OnDisable()
    {
        Application.RegisterLogCallback(null);
    }
    void HandleLog(string logString, string stackTrace, LogType type)
    {
        LogObject o = new LogObject();
        o.output = logString;
        o.stack = stackTrace;
        o.logType = type;
        if (lo.Count > 200)
        {
            lo.RemoveAt(0);
        }
        lo.Add(o);
    }
    void OnGUI()
    {
        if (m_showLogWin)
        {
            m_logWinRect = GUI.Window(0, m_logWinRect, LogWin, "输   出");
        }
        else
        {
            m_minBtnRect = GUI.Window(1, m_minBtnRect, MinBtnWin, "输   出");
        }
    }

    void MinBtnWin(int id)
    {
        GUILayout.Space(5);
        if (GUILayout.Button("展   开"))
        {
            m_showLogWin = !m_showLogWin;
        }
        GUI.DragWindow(new Rect(0, 0, 1000, 20000));
    }

    void LogWin(int id)
    {
        scrollPosition = GUILayout.BeginScrollView(scrollPosition, false, true, GUILayout.Width(585), GUILayout.Height(350));
        foreach (LogObject item in lo)
        {
            if (item.logType == LogType.Log && !m_showLog)
            {
                continue;
            }
            if (item.logType == LogType.Warning && !m_showWarning)
            {
                continue;
            }
            if (item.logType == LogType.Error && !m_showError)
            {
                continue;
            }
            switch (item.logType)
            {
                case LogType.Log:
                    GUI.color = Color.white;
                    break;
                case LogType.Warning:
                    GUI.color = Color.yellow;
                    break;
                case LogType.Error:
                    GUI.color = Color.red;
                    break;
                default:
                    GUI.color = new Color(.3f, .3f, .3f, 1f);
                    break;
            }
            GUILayout.Label(item.output + "\n" + item.stack);
            GUILayout.Space(-25);
            GUI.color = Color.gray;
            GUILayout.Label("------------------------------------------------------------------------------------------------------------------------------------------");
            GUILayout.Space(-10);
        }
        GUILayout.EndScrollView();
        GUILayout.BeginHorizontal();
        GUI.color = Color.white;
        if (GUILayout.Button("折   叠"))
        {
            m_showLogWin = !m_showLogWin;
            m_minBtnRect.x = m_logWinRect.center.x - m_minBtnRect.x / 2;
            m_minBtnRect.y = m_logWinRect.center.y - m_minBtnRect.y / 2;
        }
        if (GUILayout.Button("清   除"))
        {
            lo.Clear();
        }
        m_showLog = GUILayout.Toggle(m_showLog, "日   志");
        m_showWarning = GUILayout.Toggle(m_showWarning, "警   告");
        m_showError = GUILayout.Toggle(m_showError, "错   误");
        GUILayout.EndHorizontal();
        GUI.DragWindow(new Rect(0, 0, 1000, 20000));
    }
}

挂载相机上

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1136255
帖子:227251
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP