通过脚本控制 Profiler。
您可以使用 Profiler.BeginSample 和 Profiler.EndSample 在脚本中添加自定义性能分析器部分。
在独立平台上,您可以将所有性能分析信息保存到文件中,以便稍后进行检查。为此,您必须指定 Profiler.logFile 并将 Profiler.enabled 和 Profiler.enableBinaryLog 都设置为 true
。
由于使用性能分析器会对应用程序的性能产生负面影响,因此大多数性能分析器 API 功能仅在启用了“Development Build”时才可用。这意味着如果要在构建的应用程序中使用性能分析器 API 方法,则必须启用“Developer Build”。禁用“Development Build”会使您的应用程序运行更快,但会阻止您使用大多数性能分析器 API 方法。
例外情况是与内存使用相关的性能分析器 API 方法。由于 Unity 在运行时管理其大部分系统内存,可以在不降低性能的情况下提供该信息,因此即使未启用“Development Build”,这些方法也可用。这适用于几乎所有与内存相关的性能分析器 API 方法,但 Profiler.GetAllocatedMemoryForGraphicsDriver 和 Profiler.GetRuntimeMemorySizeLong 除外,因为后两者需要额外的性能分析数据,而这些数据仅在开发版中提供。
areaCount | 您可以进行性能分析的性能分析器区域数量。 |
enableAllocationCallstacks | 启用托管分配的调用栈的记录。 |
enableBinaryLog | 启用将性能分析数据记录到文件中的功能。 |
enabled | 启用性能分析器。 |
logFile | 指定写入性能分析数据时要使用的文件。 |
maxUsedMemory | 设置性能分析器用于缓冲数据的最大内存量。该属性以字节为单位来表示。 |
usedHeapSizeLong | 返回 Unity 已分配的字节数。这不包括外部库或驱动程序分配的字节。 |
AddFramesFromFile | 显示性能分析器中记录的性能分析数据。 |
BeginSample | 开始使用自定义标签分析一段代码。 |
BeginThreadProfiling | 在调用此方法的线程上启用性能分析。 |
EmitFrameMetaData | 将与当前帧关联的元数据写入性能分析器流。 |
EmitSessionMetaData | Write metadata associated with the whole Profiler session capture. |
EndSample | 结束当前的性能分析样本。 |
EndThreadProfiling | 释放性能分析器用于线程的内部资源。 |
GetAllCategories | Returns all ProfilerCategory registered in Profiler. |
GetAllocatedMemoryForGraphicsDriver | 返回为图形驱动程序分配的内存大小(以字节为单位)。仅适用于开发版本的播放器和编辑器。 |
GetAreaEnabled | 返回的结果表明当前是否启用了某个给定的 ProfilerArea。 |
GetCategoriesCount | Returns number of ProfilerCategory registered in Profiler. |
GetMonoHeapSizeLong | 返回托管内存的预留空间大小。 |
GetMonoUsedSizeLong | Gets the allocated managed memory for live objects and non-collected objects. |
GetRuntimeMemorySizeLong | 收集 Unity 对象使用的本机内存。 |
GetTempAllocatorSize | 返回临时分配器的大小。 |
GetTotalAllocatedMemoryLong | The total memory allocated by the internal allocators in Unity. Unity reserves large pools of memory from the system; this includes double the required memory for textures becuase Unity keeps a copy of each texture on both the CPU and GPU. This function returns the amount of used memory in those pools. |
GetTotalFragmentationInfo | Returns heap memory fragmentation information. |
GetTotalReservedMemoryLong | Unity 预留的总内存。 |
GetTotalUnusedReservedMemoryLong | 当 Unity 需要分配内存时,Unity 会在池中分配内存以供使用。此函数返回这些池中未使用的内存量。 |
IsCategoryEnabled | Returns whether or not a given ProfilerCategory is currently enabled. |
SetAreaEnabled | 启用或禁用某个给定的 ProfilerArea。 |
SetCategoryEnabled | Enable or disable a given ProfilerCategory. |
SetTempAllocatorRequestedSize | 设置临时分配器的大小。 |
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.