AsyncReadManagerMetrics 类用于监控运行时资源加载和文件读取性能。该类记录了有关由 AsyncReadManager 管理的所有文件读取操作的数据。
Unity 引擎使用 AsyncReadManager 在运行时读取大多数文件。使用 AsyncReadManager 加载的文件包括 AssetBundles、Addressables 和 Resources。此外,您可以使用 AsyncReadManager.Read 从脚本加载文件。
AsyncReadManagerMetrics 类允许您启用指标收集并检索记录的指标数据。您还可以过滤和汇总记录的数据以帮助您进行分析。该指标信息可帮助您识别涉及资源加载的问题区域,并衡量您的更改对资源加载性能的影响。
AsyncReadManagerMetrics 类仅在开发版中提供。您必须将任何对 AsyncReadManagerMetrics API 的调用保护在使用 ENABLE_PROFILER
符号的 #if 预处理器指令内。ENABLE_PROFILER
符号仅针对开发版定义,这可使您的脚本在指标不可用的版本中编译。为了向后兼容,您还可以使用 UNITY_2020_2_OR_NEWER
符号从早期版本的 Unity 中去除指标代码。例如:
# if ENABLE_PROFILER && UNITY_2020_2_OR_NEWER
AsyncReadManagerMetrics.StartCollectingMetrics();
# endif
您必须启用指标收集才能开始记录数据。使用以下方法之一启用指标收集:
注意: 启动 Unity 编辑器应用程序时,您可以通过传递 -enable-file-read-metrics 在进入运行模式时启用收集。但是,编辑器本身会加载某些类别的资源(如纹理),并且不会在您进入运行模式时重新加载它们。要全面了解文件 IO 指标,您必须从应用程序的开发版中收集数据。
通过调用 GetMetrics 检索由 AsyncReadManagerMetrics 类收集的指标。向此函数传递 AsyncReadManagerMetrics.Flags 指定是否在检索后清除指标。清除指标会将所有已完成(包括取消和失败)的读取从存储的指标中删除。清除不会影响任何排队中或进行中的操作;您可以之后通过再次调用 GetMetrics 获取未完成操作的指标。通过定期清除指标,您可以避免重复读取相同的数据,还可以减少系统的数据开销。
# if ENABLE_PROFILER && UNITY_2020_2_OR_NEWER
AsyncReadManagerRequestMetric[] metrics
= AsyncReadManagerMetrics.GetMetrics(AsyncReadManagerMetrics.Flags.ClearOnRead);
# endif
指标数据会尽可能包括有关读取操作的上下文信息。这些信息包括:请求该读取操作的 AssetLoadingSubsystem、AssetName 以及 AssetTypeID。当 AsyncReadManager 无权访问此信息时,这些指标字段的值分别为:Other、空值和 0。
__注意:__目前已知支持的资源 TypeID 如下表所示。对于可能出现的其他 TypeID,请参阅 YAML 类 ID 参考。
TypeID | 类型名 |
---|---|
28 | Texture2D |
117 | Texture3D |
89 | CubeMap |
43 | Mesh |
您可以使用以下方法获取 AsyncReadManager 指标的汇总:
例如:
# if ENABLE_PROFILER && UNITY_2020_2_OR_NEWER
AsyncReadManagerSummaryMetrics summaryOfMetrics
= AsyncReadManagerMetrics.GetCurrentSummaryMetrics(AsyncReadManagerMetrics.Flags.ClearOnRead);
# endif
或者:
# if ENABLE_PROFILER && UNITY_2020_2_OR_NEWER
AsyncReadManagerRequestMetric[] metrics
= AsyncReadManagerMetrics.GetMetrics(AsyncReadManagerMetrics.Flags.ClearOnRead);
AsyncReadManagerSummaryMetrics summaryOfMetrics
= AsyncReadManagerMetrics.GetSummaryOfMetrics(metrics);
# endif
指标的汇总数据包括以下统计信息:
(请参阅 AsyncReadManagerSummaryMetrics 获取完整列表。)
默认情况下,汇总统计信息包括所有读取操作,其中包含排队中或进行中的读取操作。您可以使用过滤器将汇总限制为您特别感兴趣的那些操作。例如,您可以使用过滤器将汇总统计数据限制为已完成的纹理资源读取操作。
注意: 计算汇总统计确实需要处理资源。为了防止这些计算改变您正在记录的测量值,您可以先收集指标,然后在您分析的操作完成后才获取汇总。
通过下列方式之一使用 AsyncReadManagerMetricsFilters 可指定要汇总的数据类型。任何与过滤器不匹配的指标都将从汇总中排除。您可以使用以下类别进行过滤:
您可以为同一个过滤器设置多个类别。读取操作必须匹配所有类别才能使其指标包含在汇总中。例如,您可以为一个过滤器指定 ProcessingState 和 Subsystem 值,仅汇总由指定子系统启动、处于指定处理状态中的操作。
您还可以为一个类别指定多个值。在这种情况下,读取操作可以匹配为该类别指定的任何值,使其指标包含在汇总中。例如,您可以为 Subsystem 类别指定 Mesh 和 Texture,汇总网格和纹理资源的操作。
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.