要在目标发布平台上分析您的应用程序,请将目标设备连接到网络或通过线缆直接连接到计算机。也可以直接在 Unity Editor 中对应用程序进行性能分析,从而在应用程序开发期间获得大致性能分析结果。
只能以开发版形式对您的应用程序进行性能分析。要进行此设置,请打开 Build Settings (File > Build Settings),然后选择应用程序的目标平台。启用 Development Build 设置。启用此设置后,与性能分析器相关的以下两个设置将变为可用状态:__Autoconnect to Profiler__ 和 Deep Profiling Support。
启用 Autoconnect Profiler 设置后,Unity Editor 在构建过程中将自己的 IP 地址烘焙到已构建的播放器中。播放器启动时会尝试将性能分析器连接到位于所烘焙的 IP 地址的 Editor。
如果还启用了 Deep Profiling Support 设置,Unity 将在已构建的播放器启动时进行深度性能分析。此功能可用于对应用程序的启动时间进行深度性能分析。这种情况下会增加少量的构建开销。
构建并运行应用程序时,播放器会显示在 Profiler 窗口的 Attach to Player 下拉选单中。Attach to Player 下拉选单会显示本地网络上运行的所有 Unity 播放器。通过播放器类型和运行播放器的主机名可以识别这些播放器(例如 iPhonePlayer (Toms iPhone))。
选择播放器,然后单击 Record 即可开始收集应用程序的性能分析信息。如果在 Build Settings 中已启用 __Autoconnect to Profiler__,Unity 会在应用程序启动时自动开始收集数据。
要在应用程序运行时持续收集数据,请启用 Player Settings 中的 Run In Background 设置(菜单:__Edit > Project Settings > Player > Resolution and Presentation__)。启用此设置后,即使让应用程序在后台运行,性能分析器也会收集数据。如果禁用此设置,性能分析器仅在应用程序在活动窗口中运行时才收集数据。
如果使用 Profiler 窗口在 Editor 中运行应用程序并进行性能分析,则结果只是该应用程序在目标平台上运行时的行为的近似值。因为运行模式与 Editor 的运行过程相同,所以无法将应用程序的 CPU、GPU 和内存使用量数据与 Unity 的相应数据完全隔离。这会进一步导致得出的性能分析数据出现误差。
为了获得更好的性能分析结果,应始终在目标设备上对应用程序进行性能分析。请只在 Editor 中进行性能分析以便快速迭代设备上已经确定的问题。
可以在 WebGL 中使用 Unity Profiler,但不能通过 Editor 连接到运行 WebGL 的播放器。这是因为 WebGL 使用 WebSocket 进行通信,所以不允许浏览器端的传入连接。要连接到运行的播放器,必须在 Build Settings__(菜单:__File > Build Settings__)中启用 Autoconnect Profiler__ 复选框。Unity 无法对 WebGL 的绘制调用进行性能分析。
iOS 和 Android 设备均支持通过网络进行远程性能分析。如果使用了防火墙,请在防火墙的出站规则中开放端口 54998 至 55511。这些是 Unity 用于远程性能分析的端口。
注意:有时,在设置远程性能分析后,Unity Editor 可能无法自动连接到设备。如果发生此情况,可以手动发起性能分析器连接。为此,请选择 Profiler 窗口中的 Attach to Player 下拉菜单,然后选择相应设备。
也可以将目标设备直接插入计算机以避免网络或连接问题。
要对 iOS 设备启用远程性能分析,请遵循以下步骤:
Android 设备支持两种远程性能分析方法:通过 WiFi 或通过 Android Debug Bridge (adb)。
要进行 WiFi 性能分析,请遵循以下步骤:
注意:Android 设备和主机(运行 Unity Editor)必须位于同一子网上才能正常进行设备检测。
要进行 Android Debug Bridge (adb) 性能分析,请遵循以下步骤:
选择 Build & Run 时,Unity Editor 会自动为应用程序创建 adb 隧道。如果要对另一应用程序进行性能分析,或者重新启动 adb 服务器,则必须手动配置此隧道。为此,请打开终端窗口或 CMD 提示窗口,并输入:
通过 USB 线缆建立编辑器到 Android 的连接时需要
adb forward tcp:34999 localabstract:Unity-{insert bundle identifier here}
通过 USB 线缆建立 Android 到编辑器的连接时需要
adb reverse tcp:34998 tcp:34999
要将深度性能分析用于 Android 构建,必须启用 Android Player Settings 中的 Mono Scripting Backend 设置(菜单:__Edit > Project Settings > Player > Android > Other Settings__),然后输入以下内容以便通过 adb 命令来启动游戏:
~$ adb shell am start -n {insert bundle identifier here}/com.unity3d.player.UnityPlayerActivity -e 'unity' '-deepprofiling'
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.