Analysis on the Slow Loading of AnimationClip under PC
Problem Description
In order to solve the problem of the redundancy of the animation FBX Mesh, the project took measures to cut out the animation, which solved this problem, it also brought new problems at the same time. That is, these animations are very slow to load under the PC.
Cause
This is mainly related to Unity’s resource management mechanism.
Problem Explanation
A few things to know about Unity:
1) All objects are inherited from UnityEngine.Object can be serialized by Unity.
2) Unity’s serialization supports multiple formats, the most commonly used is Yaml text format with better readability. After packaging, Unity will convert to a binary format with better performance. Unity provides a tool for converting binary serialized files to text serialized files. It can be found in the installation directory of Unity, such as D:\Program Files\Unity2018.4.0f1\Editor\Data\Tools\binary2text.exe.
3) The default serialization format in the editor is a text format, which is convenient for version merging. It can be modified through the editor settings.
4) Unity’s resource import operation is actually to generate Unity’s corresponding Object based on the resource source file and serialize it to the Library directory. When all resources recognized by Unity are imported, Unity will be converted into Unity internal objects (Mesh, Texture, AnimationClip…).
5) Unity’s resource loading is actually to deserialize these serialized files.
6) FBX is considered as a Prefab by Unity.
7) For each imported resource file, Unity will generate a GUID and store this GUID in the same name .meta file. As shown in the following figure:
8) Through this GUID, you can find the serialization file of the Unity object corresponding to the resource file. The method is: take the first two digits of GUID, as above: “ae”, find the directory with this name in “Library\metadata” in the project directory, and then you can find the corresponding binary serialized file with GUID as the file name.
Let’s do an experiment first to reproduce this problem: For more details, you can check the above official webpage.
1) First, prepare 4 FBX files with animation, and then in the Project panel, Ctrl+D will cut out the animations in FBX one by one.
2) Make two Prefabs and name them fbx_anim and std_anim. The Animation component in fbx_anim references the AnimationClip in 4 FBXs. The Animation component in std_anim references 4 cut-out animation files.
3) Write a test script and launch the game for testing.
4) In order to understand the impact of binary format and text format on loading performance, we conducted experiments on the two formats separately, and each experiment took 10 sets of loading time-consuming data (unit: ms). The comparison is as follows:
It can be seen that in the case of text format, the loading time of the cut-out animation is 50 times more than FBX, while in the case of binary format, the average time consumption is about 2 times that of FBX.
Question: So why is it so time-consuming to load after cutting out AnimationClip from FBX?
- Let’s take the above anim_1.anim as an example. Let’s see what the cutout AnimationClip looks like in Unity’s Library.
Execute the command to convert anim_1.anim. Anim corresponding serialized file to text format:
After opening it, you can see that this serialized file does not save the curve data of the animation. Instead, it points to the animation source file “Assets/Animations/anim_1.anim”.
- Then we find the serialized file corresponding to anim_1.fbx.
As you can see, it contains curve data information, which stores the serialized data of the AnimationClip object, as well as other object serialized data (Mesh, GameObject…).
Use the non-AssetBundle mode to run under the PC, the resource loading interface in the project is the Asset database.LoadAssetAtPath of Unity used. It is not difficult to see from the above that if the AnimationClip is in FBX, then the loaded document is the serialized binary file and when it exists as an independent form of .anim, Unity loads the animation source file directly, not the serialized binary file. Since Unity’s default serialization format setting is a text format, it will be very slow.
Question: So why doesn’t Unity save the cutout animation into the final binary serialized format?
Question: So what is the difference between this independent animation source file and the final animation file?
Yesterday, some friends asked me some questions related with this content, please feel free to leave your comments, I will keep updating game technology details here. Good luck to all my Russian friends. Many thanks for your support. :)