Unity Performance Optimization Ⅳ – Loading and Resource Management
As the engine itself, hardware equipment, production standards, etc. have been upgraded in recent years, UWA has also continued to update and output optimization rules and methods to the majority of developers. As an “upgraded version” of the performance optimization manual, [Unity Performance Optimization Series] will try to express in a simple and understandable way, so that more developers can use it. This installment will share the knowledge of loading and resource management.
Some teams often ask: Why is my game loading so slow, can it be possible to cut scenes like other games? What is the reason why the game makes the device temperature rise? In order to prevent the game that you have worked so hard from getting stuck on the real device or the turn into a hand warmer, we must make full use of the loading module and resource management module in the UWA report, and we will analyze it step by step in the future.
The Concerns of the Loading Module
Let’s start with some loading-related concerns. The following picture is the tab of the load module in the Overview report. We can see that there are several important parameter indicators on the left:
1. Loading.UpdatePreloading
This is the main loading function of the Unity engine. This item generally costs a lot when switching scenes or when loading resources asynchronously. Generally speaking, the more resources to load and the more complicated it is the more time-consuming Load.UpdatePreloading is.
Before optimizing this function, it is recommended to locate the time-consuming bottleneck first. Through the reported CPU call stack, you can view the detailed stack trend of the function in the running process, and the time-consuming allocation of the function is clear at a glance so that it can be optimized in a targeted manner.
2. UnloadUnusedAssets
This function refers to unloading unused resources. The cost mainly depends on the number of Assets and Objects in the scene. The more the quantity, the higher the time-consuming. When optimizing performance, in addition to the time-consuming peak, we also need to pay attention to the number of calls to this function.
Generally, the engine will be automatically called once the scene switches during the process. UWA recommends calling it manually every 10-15 minutes. When the game is running, the development team can try to remove a resource that has been determined to be no longer used by Resources.UnloadAsset. This API is very efficient for removing a single resource, and it can also reduce the pressure during the unified processing of Resources.UnloadUnusedAssets.
The following figure shows the stack information of loading-related functions in the report. The GarbageCollectAssetsProfile in the stack is caused by calling Resources.UnloadAssetsUnused. If the occupancy of this item is too high, you need to pay attention to whether you call Resources.UnloadUnusedAssets too frequently.
3. GC.Collect
The frequency of GC calls is mainly affected by the reserved mono. The more and more the reserved mono allocation of the function, the faster the GC will come. Therefore, when our GC.Collect function is called more frequently (as shown in the figure below), especially as the game running time increases and becomes more and more frequent, we need to pay attention to whether there are functional operations with high allocation and frequently reserved mono allocation. In this part, you can use the Mono mode of GOT Online to check whether there is too fast or too high Mono distribution.
4. Instantiate
The statistics here are the time-consuming resource instantiation. When the projects’ resources are more complex and the number of instantiations is greater, the lag will be more obvious, but this part is often easily overlooked by everyone. How does the UWA handle this part of the problem? In the following content, we will explain in detail by combining the [Resource Management] module in the UWA real device test report.
Asset Management
Here, asset management refers to the asset call frequency, time-consuming and other strategies, because there is nothing more than two angles that affect the loading experience: the frequency of loading and the time consumption of each loading. In the real device test report, we can see that the label of [Resource Management] contains the following test items:
Please refer the official contents on their official webpage. Great thanks for supporting. :)