CPU Usage Profiler 显示游戏中的时间使用情况。选择此选项后,下方面板将显示所选帧的层级时间数据。请参阅有关 Profiler 窗口的文档以了解关于 Profiler 时间轴的更多信息。
上下拖动图表标签可重新排列 CPU 图表的堆叠方式。
在下方面板中选择某一项时,将突出显示其对 CPU 图表的贡献(其余部分将变暗)。再次单击某一项将取消选择该项。
在层级时间数据中,__Self__ 列表示在特定函数中所用的时间量,不包括调用子函数所用的时间。在上面的截屏中,1.9% 的时间用在了 Camera.Render
函数上。此函数在此帧做了一些工作,并调用各种绘制和剔除函数。排除所有这些函数之后,只有 0.4% 的时间用在了 Camera.Render
函数本身上。
Time ms 和 Self ms 列显示相同的信息,但以毫秒为单位。Camera.Render
需要 0.06ms,但包括其调用的所有函数在内一共需要 0.31ms。GC Alloc 列显示当前帧中已分配并稍后由垃圾回收器收集的内存量。将此值保持为零可防止垃圾回收器造成帧率不稳。
CPU 性能分析器的 Others 部分记录不属于 Rendering、Scripts、Physics、Garbage Collection 或 VSync 的所有方面的总和。此部分包括 Animation、AI、Audio、Particles、Networking、Loading 和 PlayerLoop。
下面的描述简要介绍了各种高级 Physics Profiler 标记的含义。
FixedUpdate
中调用。通过指示物理引擎 (PhysX) 运行其模拟来更新物理系统的当前状态。FixedUpdate
中调用。处理所有非布料物理作业。扩展此标记可显示物理引擎内部完成的工作的低级细节。FixedUpdate
中调用。处理所有布料物理作业。扩展此标记将显示物理引擎内部完成的工作的低级细节。FixedUpdate
中调用。从物理引擎收集物理模拟结果。FixedUpdate
中调用。更新所有物理体的位置和旋转以及发送说明这些更新的消息。FixedUpdate
中调用。此阶段在物理 FixedUpdate
结束后运行,负责处理响应模拟结果的所有不同阶段。更新接触、关节破坏和触发器并发送相关消息。包括四个不同的子阶段:
FixedUpdate
中调用。处理 OnTriggerEnter
和 OnTriggerExit
事件。FixedUpdate
中调用。处理 OnTriggerStay
事件。FixedUpdate
中调用。处理 OnCollisionEnter
、OnCollisionExit
和 OnCollisionStay
事件。FixedUpdate
中调用。处理与被破坏的关节相关的更新和消息。Update
中调用。进行与布料及其蒙皮网格相关的更新。Update
中调用。此阶段处理所有物理对象的位置和旋转的插值。CPU 性能分析器能够检测并警告一些常见的性能问题。查看 CPU Usage 时,这些问题会显示在下方面板的 Warning 列中。
性能分析器可检测的具体问题包括:
在上面的截屏中,性能分析器显示了 Static Collider.Move 警告。Warning 列显示此警告已在当前帧中触发 12 次。术语“延迟成本”意味着,虽然性能分析器中的条目可能显示低成本(在此情况下为 0.00ms),但该操作可能会在以后触发更多消耗系统资源的操作。
本机内存性能分析允许在 Unity 的本机内存管理系统中分析活动,并评估对运行时性能的影响。在 Unity 的内存管理中搜索不必要的或资源密集的分配模式时,此分析功能非常有用。
要分析 Unity 的本机内存管理,必须记录内存情况。要访问本机内存记录模式(在 Unity 中称为 Mem Record__),请选择 Window__ > Analysis > Profiler 以打开 Profiler 窗口。选择 CPU Usage Profiler(如果不可见,请单击 Add Profiler > CPU__),然后在 Profiler 下面选择下拉菜单。接下来,单击 Timeline__,然后选择 Mem Record。
选项 | 功能 | 对性能的影响 |
---|---|---|
None | 禁用模式。这是默认选择。 | 无 |
Sample only | 记录内存分配、重新分配、取消分配、活动类型和系统。 | 低 |
Callstack (fast) | 此选项具有与 Sample only 相同的功能,但还记录来自本机分配站点的快捷调用栈(调用栈从本机符号转换为脚本符号)。实际上,最多只能看到最深脚本符号的调用栈。 | 中 |
Callstack (full) | 此选项具有与 Sample only 相同的功能,但还记录具有完整脚本到本机转换和本机到脚本转换的调用栈。 | 高 |
注意:当激活的性能分析器仅连接到独立平台播放器时,仅支持低影响力的 Sample only 模式。
记录的内存分配样本以亮红色显示在 Profiler 窗口中。
单击 Mem Record 旁边的 High Detail 按钮可启用 High Detail 模式。选择一个样本来显示分配类型和系统。如果为所选分配样本记录了调用栈,则还会解析并显示关联的调用栈符号:
在许多情况下,__Mem Record__ 功能很有用。例如:
CPU Usage Profiler Timeline 的 High Detail 视图为 Unity 的 CPU Usage Profiler 记录的每个样本提供至少一个宽度像素。
这样便可查看帧中所有活动的完整概况,包括线程同步或内存分配等短期活动。
要启用 High Detail 视图,请选择 Window > Analysis > Profiler 以打开 Profiler 窗口。选择 CPU Usage Profiler(如果不可见,请单击 Add Profiler > CPU__),然后将在 Profiler 下面选择下拉菜单,并依次单击 Timeline__ 和 High Detail。
以下两个图显示了 CPU Usage Profiler Timeline 的 High Detail 视图与普通视图之间的区别。
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.