Unity iOS 和 Android 包含内置性能分析器。内置的性能分析器从设备上运行的游戏发出控制台消息。这些消息每 30 秒写入一次,可用于分析游戏的运行情况。了解这些消息的含义并非总是那么容易,但至少,您应该能够快速地确定游戏受限于 CPU 还是 GPU。如果受限于 CPU,那么是脚本代码还是 Mono 垃圾收集让游戏速度变慢。请参阅本页后面的内容,了解如何配置内置的性能分析器。
以下是内置性能分析器的输出示例。
iPhone/iPad Unity internal profiler stats:
cpu-player> min: 9.8 max: 24.0 avg: 16.3
cpu-ogles-drv> min: 1.8 max: 8.2 avg: 4.3
cpu-waits-gpu> min: 0.8 max: 1.2 avg: 0.9
cpu-present> min: 1.2 max: 3.9 avg: 1.6
frametime> min: 31.9 max: 37.8 avg: 34.1
draw-call #> min: 4 max: 9 avg: 6 | batched: 10
tris #> min: 3590 max: 4561 avg: 3871 | batched: 3572
verts #> min: 1940 max: 2487 avg: 2104 | batched: 1900
player-detail> physx: 1.2 animation: 1.2 culling: 0.5 skinning: 0.0 batching: 0.2 render: 12.0 fixed-update-count: 1 ..2
mono-scripts> update: 0.5 fixedUpdate: 0.0 coroutines: 0.0
mono-memory> used heap: 233472 allocated heap: 548864 max number of collections: 1 collection total duration: 5.7
所有时间均以毫秒/帧为单位。可以查看最近三十帧的最短时间、最长时间和平均时间。
属性__: |功能__: | |
---|---|
cpu-player | 显示游戏在 Unity 引擎中执行代码以及在 CPU 上执行脚本所用的时间。 |
cpu-ogles-drv | 显示在 CPU 上执行 OpenGL ES 驱动程序代码所用的时间。绘制调用的数量、内部渲染状态变化的数量、渲染管线设置甚至已处理顶点的数量等大量因素都会影响驱动程序的统计信息。 |
cpu-waits-gpu | 显示 CPU 在等待 GPU 完成渲染时处于空闲状态的时间。如果此数字超过 2–3 毫秒,那么应用程序很可能受限于填充率/GPU 处理。如果该值太小,则性能分析中不会显示该值。 |
msaa-resolve | 应用抗锯齿所用的时间。 |
cpu-present | 在 OpenGL ES 中执行 presentRenderbuffer 命令所用的时间。 |
frametime | 表示一个游戏帧的总时间。请注意,iOS 硬件始终锁定在 60Hz 的刷新率,因此您将始终得到 16.7ms (1000ms/60Hz = 16.7ms) 的倍数。 |
属性__: |功能__: | |
---|---|
tris # | 为渲染发送的三角形总数。 |
verts # | 为渲染发送的顶点总数。如果仅使用静态几何体,应将此数字保持在 10000 以下,但是如果有大量的蒙皮几何体,应将其设置为小得多的值。 |
batched | 由引擎自动批处理的绘制调用、三角形和顶点的数量。将这些数字与绘制调用和三角形总数比较,就会知道场景的批处理准备状态。为改善批处理性能,应当在对象之间尽可能多分享材质。 |
player-detail 部分提供引擎内部情况的详细分类:
属性__: |功能__: | |
---|---|
physx | 在物理方面所用的时间。 |
animation | 为骨骼生成动画时所用的时间。 |
culling | 剔除摄像机视锥体外的对象所用的时间。 |
skinning | 将动画应用于蒙皮网格所用的时间。 |
batching | 对几何体进行批处理所用的时间。与静态几何体的批处理相比,动态几何体的批处理成本要高得多。 |
render | 对可见对象进行渲染所用的时间。 |
fixed-update-count | 在此帧期间执行的 FixedUpdates 的最小和最大数量。过多的 FixedUpdates 将大大降低性能。 |
mono-scripts 部分提供 Mono 运行时期间执行代码所用的时间的详细分类:
属性__: |功能__: | |
---|---|
update | 脚本中执行所有 Update() 函数所用的总时间。 |
fixedUpdate | 脚本中执行所有 FixedUpdate() 函数所用的总时间。 |
coroutines | 在脚本协程内所用的时间。 |
mono-memory 部分让您了解 Mono 垃圾回收器如何管理内存:
属性__: |功能__: | |
---|---|
allocated heap | 可用于分配的内存总量。如果堆中没有足够的内存用于给定的分配,将触发垃圾收集。如果即使在收集之后仍然没有足够的可用内存,那么分配的堆的大小将增大。 |
used heap | 在 allocated heap 中当前已由对象使用的部分。每次创建新类实例(不是结构)时,该数字都会增长,直到进行下一次垃圾收集。 |
max number of collections | 最近 30 帧期间的垃圾收集执行次数。 |
collection total duration | 在最近 30 帧期间发生的所有垃圾收集过程的总时间(毫秒)。 |
在 iOS 上,内部性能分析器默认处于禁用状态。要启用,请打开 Unity 生成的 XCode 项目,选择 InternalProfiler.h
文件,将以下行
#define ENABLE_INTERNAL_PROFILER 0
更改为
#define ENABLE_INTERNAL_PROFILER 1
选择 XCode 菜单中的 View > Debug Area > Activate Console 以显示输出控制台 (GDB),然后运行您的项目。Unity 每 30 帧就将统计信息输出到控制台窗口。
若要在 Android 上启用内部性能分析器,单击 Player 窗口(__Edit__ > Project Settings__,然后选择 Player__ 类别)中的 Enable Internal Profiler__(已弃用)复选框。构建时,确保已在 Build Settings__ 中选中 Development Build__,然后在设备上运行时应该会在 logcat 中显示统计信息。需要 adb__ 或 Android Debug Bridge 才能查看 logcat。拥有这些工具后,只需运行 shell 命令 adb logcat。
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.