Visual Studio 不支持调试 Unity WebGL 内容。为了帮助您准确了解内容发生的问题,以下提供了有关如何从构建中获取信息的一些提示。
Unity WebGL 无权访问您的文件系统,因此不会像其他平台那样写入日志文件。但是,它会将所有日志记录信息(例如 Debug.Log
、Console.WriteLine
或 Unity 的内部日志记录)写入浏览器的 JavaScript 控制台。
要打开 JavaScript 控制台,请执行以下操作:
出于调试目的,您可能希望在 Unity 中构建开发版(打开 Build Settings 窗口并选中 Development Build 复选框)。使用开发版可以连接性能分析器,且 Unity 不会缩小开发版,因此发出的 JavaScript 代码仍然包含人类可读(虽然进行了 C++ 重整)的函数名称。当遇到浏览器错误时,使用 Debug.LogError
时,或者抛出异常并且禁用了异常支持时,浏览器可以使用它们来显示堆栈跟踪。与具有完全异常支持(见下文)时可能发生的托管堆栈跟踪不同,这些堆栈跟踪具有重整的名称,不仅包含托管代码,还包含内部 UnityEngine 代码。
WebGL 具有不同级别的异常支持(请参阅有关 WebGL 构建的文档)。默认情况下,Unity WebGL 仅支持显式抛出的异常。您可以启用__完全__的异常支持,这种情况下支持在 IL2CPP 生成的代码中发起额外的检查,从而检测是否访问了托管代码中的 null 引用和越界数组元素。这些额外的检查会显著影响性能并增加代码大小和加载时间,因此只应将其用于调试。
__完全__的异常支持还会发出函数名称,以便为托管代码生成堆栈跟踪。出于这一原因,对于未捕获的异常和 Debug.Log
语句,堆栈跟踪将出现在控制台中。使用 System.Environment.Stacktrace
可获取堆栈跟踪字符串。
这是一个常见问题,尤其是在 32 位浏览器上。如需了解 WebGL 内存问题以及如何修复这些问题的更多信息,请参阅有关 WebGL 中的内存的文档。
Unity WebGL 将会话之间必须保留的所有文件(例如 PlayerPrefs 或保存在 persistentDataPath 中的文件)存储到浏览器 IndexedDB 中。这是一个异步 API,因此您不知道它何时完成。
调用以下代码可以确保 Unity 将所有挂起的文件系统写入操作从内存中刷新到 IndexedDB 文件系统:
FS.syncfs(false, function (err) {
console.log('Error: syncfs failed!');
});
浏览器控制台日志通常会因错误的服务器配置而输出此错误。如需详细了解如何部署发布构建,请参阅有关部署压缩构建的文档。
当内容尝试加载使用 LZMA 压缩的 AssetBundle 而 Unity WebGL 不支持时,浏览器控制台日志会输出此错误。使用 LZ4 压缩方式重新压缩 AssetBundle 可以解决此问题。如需了解 WebGL 压缩的更多信息,请参阅有关 WebGL 构建的文档,尤其是 AssetBundles 部分。
2018–09–03 页面已修订
从 2018.1 开始,MonoDevelop 由 Visual Studio 取代
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.