是否可以在不读取控制台日志的情况下检测在 Unity 中运行的 C# 脚本中的错误?
当我必须构建游戏并在手机中测试它时,我需要这个。如果运行时出现错误,它将显示一个消息框来显示错误。
据我了解,我们可以使用Unity Log Viewer来打印设备中的所有Log。但我要求另一种方法来做到这一点。我问一个更简单的解决方案。我的解决方案我认为最好是在编辑器中成功运行时检测到小错误,但在设备中运行时会出现问题,因为它只显示 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.
如果有人明白我的意思,那么请给我一个解决方案。
谢谢
所以没有人明白我的意思。但我自己找到了解决方案。
我们可以用 :应用程序.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);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)