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: Due to so many information, please feel free to check the full content on the official webpage for more details.
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?
Please feel free to leave your comment below, I will reply as soon as possible. Many thanks for all my Russian friends or who can speak Russian. Let's know each others better. Good luck to you all!