gpt4 book ai didi

c# - 在没有看到控制台的情况下运行时如何检测unity c#是否有错误?

转载 作者:行者123 更新时间:2023-12-04 16:08:02 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Catch all exception in Unity

(3 个回答)


4年前关闭。




是否可以在不读取控制台日志的情况下检测在 Unity 中运行的 C# 脚本中的错误?
当我必须构建游戏并在手机上测试时,我需要这个。如果在运行时出现错误,它将显示一个显示错误的消息框。

我知道我们可以使用 Unity Log Viewer 打印设备中的所有日志。但我要求另一种方法来做到这一点。我问一个更简单的解决方案。我的解决方案我认为最好在编辑器中成功运行时检测小错误,但在设备中运行时会出现问题,因为它只显示 showMessageBox 错误。

我需要检测运行时是否有问题。我意识到有 Debug.LogError 我们可以检测到错误。但是 Debug.LogError 只是由我们打印消息类型以指出对象错误是什么。我需要的是检测像控制台这样的全局错误并显示来自统一引擎的错误消息。

我需要的可能是这样的:

void Update() {
showMessageBox(isErrorDetect());
}

showMessageBox => is a function to show message box.
isErrorDetect => this will print an error if detect like a console.

如果有人明白我的意思,那么请给我一个解决方案。

谢谢你

最佳答案

所以没有人明白我的意思。但我自己得到了解决方案。
我们可以使用: Application.logMessageReceived 这就是我的意思。
如果在设备上运行游戏时出现错误,则会弹出以下脚本。还没有测试。但我确定它有效。当在编辑器中运行没有错误但在移动设备上运行时出现错误时,它将为您提供帮助。
我自己做。例子 :

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class ErrorHandlePopUp : MonoBehaviour {

public Image PopUp;
string error;

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

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

void HandleLog(string logString, string stackTrace, LogType type) {

if (type == LogType.Error) {
error = error + "\n" + logString;
PopUp.gameObject.SetActive (true);
PopUp.transform.GetChild (0).GetComponent<Text> ().text = "Error";
PopUp.transform.GetChild (1).GetComponent<Text> ().text = error;
}
}

public void Dismiss() {
PopUp.gameObject.SetActive (false);
}

}

关于c# - 在没有看到控制台的情况下运行时如何检测unity c#是否有错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45341179/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com