Collect asset loading metrics
Monitor asset loading at runtime.
阅读时间4 分钟最后更新于 24 天前
Use the class to monitor runtime asset loading and file reading performance. This class records data for all file read operations managed by the .
AsyncReadManagerMetricsAsyncReadManagerUnity uses the to read most files at runtime, including AssetBundles, Addressables, and Resources. You can also load files from scripts using .
AsyncReadManagerAsyncReadManager.ReadYou can use the to do the following:
AsyncReadManagerMetrics- Start and stop metrics collection.
- Retrieve and analyze recorded metrics data.
- Filter and summarize data to help your analysis.
This information can help you identify problems with asset loading and measure the impact of your changes have on performance.
Development build requirement
The class is only available in development builds. To ensure compatibility, wrap calls to APIs in an preprocessor directive using the symbol. For older Unity versions, use the symbol to remove the metric code. For example:
AsyncReadManagerMetricsAsyncReadManagerMetrics#ifENABLE_PROFILERUNITY_2020_2_OR_NEWER#if ENABLE_PROFILER && UNITY_2020_2_OR_NEWER AsyncReadManagerMetrics.StartCollectingMetrics();#endif
Enable metric collection
Enable metrics collection before any data is recorded. You can do this in one of the following ways:
- Call in your script.
AsyncReadManagerMetrics.StartCollectingMetrics - Pass the command-line argument when launching your application.
-enable-file-read-metrics
To collect metrics in Play mode, pass the argument when launching the Unity Editor. However, the Editor loads some categories of assets, such as textures, and doesn't reload them when you enter Play mode. For complete file I/O metrics, you must collect data from a development build of your application.
-enable-file-read-metricsGetting metric data
Use to retrieve collected metrics from . Specify to determine whether to clear metrics after retrieval. Clearing metrics removes completed (including canceled and failed) reads but retains queued or in-progress operations. To access the metrics for unfinished operations, call again after they complete. By clearing the metrics regularly, you can avoid rereading the same data and also reduce the data overhead of the system.
GetMetricsAsyncReadManagerMetricsAsyncReadManagerMetrics.FlagsGetMetrics#if ENABLE_PROFILER && UNITY_2020_2_OR_NEWER AsyncReadManagerRequestMetric[] metrics = AsyncReadManagerMetrics.GetMetrics(AsyncReadManagerMetrics.Flags.ClearOnRead);#endif
Metrics data includes the following context data:
- The that initiated the read. If unavailable, defaults to
AssetLoadingSubsystem.Other - . If unavailable, defaults to empty.
AssetName - . If unavailable, defaults to zero.
AssetTypeID
The following is a list of supported asset TypeIDs:
TypeID | Type name |
|---|---|
| 28 | Texture2D |
| 117 | Texture3D |
| 89 | CubeMap |
| 43 | Mesh |
For other TypeIDs that might appear, refer to YAML class ID reference.
Get summarized data
Get a summary of the metrics with the following methods:
AsyncReadManager- : Returns a summary of all the metrics collected after the last time you cleared the metric store.
GetCurrentSummaryMetrics - : Returns a summary of the metrics in an array of
GetSummaryOfMetrics, which you can get by callingAsyncReadManagerRequestMetric.GetMetrics
For example:
#if ENABLE_PROFILER && UNITY_2020_2_OR_NEWER AsyncReadManagerSummaryMetrics summaryOfMetrics = AsyncReadManagerMetrics.GetCurrentSummaryMetrics(AsyncReadManagerMetrics.Flags.ClearOnRead);#endif
Or:
#if ENABLE_PROFILER && UNITY_2020_2_OR_NEWER AsyncReadManagerRequestMetric[] metrics = AsyncReadManagerMetrics.GetMetrics(AsyncReadManagerMetrics.Flags.ClearOnRead); AsyncReadManagerSummaryMetrics summaryOfMetrics = AsyncReadManagerMetrics.GetSummaryOfMetrics(metrics);#endif
Summary statistics include:
- Average bandwidth
- Average read size
- Asset type with the longest load time
- Number of reads
- Number of requests
- Total bytes read
For a full list of summary metrics, refer to .
AsyncReadManagerSummaryMetricsBy default, the summarized statistics include all read operations, including those that are queued or in-progress. You can use a filter to limit the summary to those operations you're interested in. For example, you can use a filter to limit the summarized statistics to completed read operations for texture assets.
Using summary filters
Use to filter summarized data. You can filter by the following:
AsyncReadManagerMetricsFilters- Asset type: By YAML ID.
- Processing state: For example, queued, reading, or completed.
- Read type: For example, async or sync.
- Priority: For example, high or low.
- Subsystem: By .
AssetLoadingSubsystem
You can set multiple categories for the same filter. A read operation must match all categories for the metrics of that operation to be included in the summary. For example, you can specify values for both the and the of a filter to summarize only operations in the designated processing states initiated by the designated subsytems.
ProcessingStateSubsystemYou can also specify multiple values for a category. In this case, a read operation can match any value you specify for a category for its metrics to be included in the summary. For example, filtering by both mesh and texture includes operations for both asset types.