debug功能自己写(3D Text)

debug功能很重要,调试程序百思不得其解的时候可以从容应对。本文自己通过unity 自带的3D Text部件实现 log信息的实时显示。

1.新建3D Text,并改名为DebugLog(名字随意)

2. 写脚本LoggingPanel.cs,namespace可以不要,我是用的被人的例子代码,懒得改了

 

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

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

namespace Microsoft.MixedReality.OpenXR.BasicSample
{
    /// <summary>
    /// Prints messages logged through the Unity Console onto a text mesh.
    /// </summary>
    public class LoggingPanel : MonoBehaviour
    {
        [SerializeField]
        private TextMesh m_text = null;

        /// <summary>
        /// The maximum number of lines to be stored for displaying on the text mesh.
        /// If this number is exceeded, old messages will be removed.
        /// </summary>
        public int MaxLines = 8;

        private Queue<string> m_loggedLines = new Queue<string>();

        void OnEnable() => Application.logMessageReceived += LogUnityMessage;
        void OnDisable() => Application.logMessageReceived -= LogUnityMessage;

        /// <summary>
        /// Add the string to a TextMesh and optionally log it to the Unity console.
        /// </summary>
        /// <param name="line">The text to log.</param>
        /// <param name="debugLog">Whether to log to the Unity console.</param>
        public void LogUnityMessage(string message, string stackTrace, LogType type)
        {
            while (m_loggedLines.Count >= MaxLines)
            {
                m_loggedLines.Dequeue();
            }
            m_loggedLines.Enqueue(message + "\n");

            if (m_text != null)
            {
                m_text.text = m_loggedLines.Aggregate(new StringBuilder(),
                    (sb, logged) => sb.Append(logged), sb => sb.ToString());
            }
        }
    }
}

3.将2中的代码拖到DebugLog上,并将DebugLog拖到LoggingPanel的Text上,如下:

然后就OK啦,所有的debug.log信息都会显示在这里啦。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值