如何在统一崩溃之前呈现 debug.log 消息
How do I render debug.log messages before crashing unity
当 unity 崩溃时 debug.log 来自上一个事件的不渲染。这恰好是导致统一崩溃的事件(可能通过循环)
到目前为止,我已经找到 Application.Quit(),但显然,如果我准确知道 unity 何时会崩溃,我只能使用它(例如找出某些(非嵌套)循环中的哪一个崩溃)
(我还发现了很多实际上没有做任何事情的信息,比如在开始游戏之前打开控制台window。虽然没有列出所有这些)
我想这个示例代码应该足够好地描述了问题:
while (condition) {
DoSomething(); //contains a loop too
Debug.Log (value);
Application.Quit ();
}
显然,只有第一次迭代发生。
但是内循环通常不会在前几次崩溃。所以我想在每次内部循环终止时记录一些值。
您可以用 try catch 围绕有问题的块。如果它抛出错误,您可以将其记录在 catch 块中。
try
{
while (condition) {
DoSomething(); //contains a loop too
Debug.Log (value);
}
}
catch
{
Debug.Log(value);
}
您可以在cmd中使用adb日志。
If you're using Unity with android, you must have already installed adb through Android SDK. Given that, yeah, it should be simple and obvious. And Ben is right. You didn't seem to google enough or take any effort here... But I just hate simple unanswered questions.
There are just a couple things to assure:
Make sure USB debugging is enabled on the Android device (check under Settings -> Development). - Run adb through the command prompt (with command line argument logcat) that comes with the Android SDK while running the game on the Android, still connected via USB to the computer.
adb logcat -s Unity ActivityManager PackageManager dalvikvm DEBUG
Finally, if the Android Debug Bridge can't help you, I'm not sure what could. There was this one time, actually, it couldn't help me... But I was ignoring a unity warning, so I did the debugging checklist in the wrong order.
Source of the answer
您可以添加一个侦听器来统一记录消息的事件 (Application.logMessageReceived
) 并将其存储在文件中或通过网络发送以防止丢失。
void Awake()
{
DontDestroyOnLoad(this);
Application.logMessageReceived += HandleLog;
}
void OnApplicationQuit()
{
Application.logMessageReceived -= HandleLog;
}
void HandleLog(string logString, string stackTrace, LogType type)
{
File.AppendAllText ("D:/Log.txt" /*Path to your log folder*/, logString + stackTrace);
}
当 unity 崩溃时 debug.log 来自上一个事件的不渲染。这恰好是导致统一崩溃的事件(可能通过循环)
到目前为止,我已经找到 Application.Quit(),但显然,如果我准确知道 unity 何时会崩溃,我只能使用它(例如找出某些(非嵌套)循环中的哪一个崩溃)
(我还发现了很多实际上没有做任何事情的信息,比如在开始游戏之前打开控制台window。虽然没有列出所有这些)
我想这个示例代码应该足够好地描述了问题:
while (condition) {
DoSomething(); //contains a loop too
Debug.Log (value);
Application.Quit ();
}
显然,只有第一次迭代发生。 但是内循环通常不会在前几次崩溃。所以我想在每次内部循环终止时记录一些值。
您可以用 try catch 围绕有问题的块。如果它抛出错误,您可以将其记录在 catch 块中。
try
{
while (condition) {
DoSomething(); //contains a loop too
Debug.Log (value);
}
}
catch
{
Debug.Log(value);
}
您可以在cmd中使用adb日志。
If you're using Unity with android, you must have already installed adb through Android SDK. Given that, yeah, it should be simple and obvious. And Ben is right. You didn't seem to google enough or take any effort here... But I just hate simple unanswered questions.
There are just a couple things to assure:
Make sure USB debugging is enabled on the Android device (check under Settings -> Development). - Run adb through the command prompt (with command line argument logcat) that comes with the Android SDK while running the game on the Android, still connected via USB to the computer.
adb logcat -s Unity ActivityManager PackageManager dalvikvm DEBUG
Finally, if the Android Debug Bridge can't help you, I'm not sure what could. There was this one time, actually, it couldn't help me... But I was ignoring a unity warning, so I did the debugging checklist in the wrong order.
Source of the answer
您可以添加一个侦听器来统一记录消息的事件 (Application.logMessageReceived
) 并将其存储在文件中或通过网络发送以防止丢失。
void Awake()
{
DontDestroyOnLoad(this);
Application.logMessageReceived += HandleLog;
}
void OnApplicationQuit()
{
Application.logMessageReceived -= HandleLog;
}
void HandleLog(string logString, string stackTrace, LogType type)
{
File.AppendAllText ("D:/Log.txt" /*Path to your log folder*/, logString + stackTrace);
}