在Unity中捕获所有异常 [英] Catch all exception in Unity

查看:837
本文介绍了在Unity中捕获所有异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用商店中有一个Unity手机游戏,并且我有一个功能可以将玩家发送的电子邮件发送到我的服务器,以通知我发生了某些事情,这是不应该发生的.如果例如我写的try失败了,我给自己发送了一封电子邮件,以查看其中一定有错误.

I've got a Unity mobile game in the app stores and I've got a function that sends emails from the players to my server, to notify me that stuff happened, that is not supposed to happen. If e.g. a try that I wrote fails, I send myself an email to see, that there must be a bug.

是否可以构建类似OnException处理程序的东西,以便在发生每个异常(包括我未编写的代码)时向我发送电子邮件?某些异常可能来自于我使用的插件,所以我想拥有某种侦听器,该侦听器会在调用任何异常时进行填充.

Is it possible, to build something like an OnException handler, that sends me an email on every exception that occurs, including code I didn't write? Some exceptions may come from plugins that I use, so I would like to have some sort of Listener, that does stuff whenever any sort of exception get's called.

有可能吗?

推荐答案

无法捕获其他插件中的所有其他异常. Unity在后台执行此操作,并且Unity为您提供API来接收其捕获的异常:

You can't catch all other exceptions in other plugins. Unity does that in the background and Unity gives you API to receive the exceptions it caught:

Unity的较新版本:

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

//Called when there is an exception
void LogCallback(string condition, string stackTrace, LogType type)
{
    //Send Email
}

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

Unity的旧版本:

void OnEnable()
 {
     Application.RegisterLogCallback(LogCallback);
 }

 //Called when there is an exception
 void LogCallback(string condition, string stackTrace, LogType type)
 {
     //Send Email
 }

 void OnDisable()
 {
     Application.RegisterLogCallback(null);
 }

还有 logMessageReceivedThreaded .

编辑:

Stacktrace问题:

如果stackTrace参数为空,则可以使用System.Diagnostics.StackTrace类在仍位于LogCallback函数内部时自己手动检索stacktrace.例如,

If the stackTrace parameter is empty then you can use the System.Diagnostics.StackTrace class to manually retrieve the stacktrace yourself while still inside the LogCallback function. For example,

void LogCallback(string condition, string stackTrace, LogType type)
{
    //Send Email

    System.Diagnostics.StackTrace sTrace = new System.Diagnostics.StackTrace();
    Debug.Log(sTrace.ToString());
}

这篇关于在Unity中捕获所有异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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