将 Debug.log 作为统一的 GUI 元素 [英] Putting Debug.log as a GUI element in unity

查看:28
本文介绍了将 Debug.log 作为统一的 GUI 元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序当前在控制台中显示文本.

My program currently displays text in the console.

我希望此文本显示在游戏窗口中.

I want this text to be displayed in the game window.

数据是网络请求的一部分.

The data is part of a web request.

是否有一种简单的方法可以将控制台中显示的内容显示为 GUI 元素?

Is there a simple way of displaying what appears in the console as a GUI element?

推荐答案

是的,您可以简单地添加一个回调到例如Application.logMessageReceivedThreaded

Yes you can simply add a callback to e.g. Application.logMessageReceivedThreaded

使用来自 这个线程

Example from the API extended with OnGUI script from this thread

// Put this on any GameObject in the scene
public class ExampleClass : MonoBehaviour
{
    // Adjust via the Inspector
    public int maxLines = 8;
    private Queue<string> queue = new Queue<string>();
    private string currentText = "";

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

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

    void HandleLog(string logString, string stackTrace, LogType type)
    {
        // Delete oldest message
        if (queue.Count >= maxLines) queue.Dequeue();

        queue.Enqueue(logString);

        var builder = new StringBuilder();
        foreach (string st in queue)
        {
            builder.Append(st).Append("\n");
        }

        currentText = builder.ToString();
    }

    void OnGUI()
    {
        GUI.Label(
           new Rect(
               5,                   // x, left offset
               Screen.height - 150, // y, bottom offset
               300f,                // width
               150f                 // height
           ),      
           currentText,             // the display text
           GUI.skin.textArea        // use a multi-line text area
        );
    }
}

<小时>

一般来说:OnGUI 是一种遗留物,你真的应该只将它用于调试.


In general: OnGUI is kind of legacy and you should really only use this for debugging.

但是您基本上也可以使用相同的脚本,例如UI.Text 组件,而不是使用 OnGUI 将文本分配给它.

But you can basically use the same script also for e.g. a UI.Text component and instead of using OnGUI assign the text to it.

脚本看起来基本相同,但有一个

The script would basically look the same but have a

public Text text;

而不是 OnGUI 会直接做

text.text = builder.ToString();

这篇关于将 Debug.log 作为统一的 GUI 元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆