【问题标题】:How do I print Unity's Debug.Log to the screen/GUI? [duplicate]如何将 Unity 的 Debug.Log 打印到屏幕/GUI? [复制]
【发布时间】:2022-01-26 06:07:37
【问题描述】:

当您在 Unity IDE 中运行项目时,您可以在单独的控制台小部件中看到日志消息。但是,我正在开发一个多人游戏,所以我需要至少在 2 个以上的客户端上进行测试,这需要在 IDE 之外进行独立构建,所以我再也看不到日志消息了。有没有办法在实际屏幕或 GUI 上打印日志?

【问题讨论】:

  • 我确实试图寻找重复但我真的找不到那个

标签: c# unity3d logging


【解决方案1】:

添加此C# script 并将其附加到场景中的新游戏对象:

using UnityEngine;
using System.Collections;

public class ZzzLog : MonoBehaviour
{
    uint qsize = 15;  // number of messages to keep
    Queue myLogQueue = new Queue();

    void Start() {
        Debug.Log("Started up logging.");
    }

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

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

    void HandleLog(string logString, string stackTrace, LogType type) {
        myLogQueue.Enqueue("[" + type + "] : " + logString);
        if (type == LogType.Exception)
            myLogQueue.Enqueue(stackTrace);
        while (myLogQueue.Count > qsize)
            myLogQueue.Dequeue();
    }

    void OnGUI() {
        GUILayout.BeginArea(new Rect(Screen.width - 400, 0, 400, Screen.height));
        GUILayout.Label("\n" + string.Join("\n", myLogQueue.ToArray()));
        GUILayout.EndArea();
    }
}

该脚本改编自here,但进行了很多清理,角落已从左上角更改为右上角,并且添加了队列大小的新变量,因此消息不会消失屏幕:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-09
    • 1970-01-01
    • 2023-03-15
    • 2021-09-20
    • 1970-01-01
    • 2021-03-12
    • 1970-01-01
    相关资源
    最近更新 更多