Previous chapter: "Unity3d Technical Document Translation" article 1.7 assetbundles patch Update
Chapters in the original chapter: "Unity Manual" → "Working in Unity" → "Advanced Development" → "assetbundles" → "Troubleshooting"
Assetbundles problem and its solving method
This section covers some common issues with projects that use Assetbundles.
Resource duplication (Asset duplication)
The Assetbundle system starting with UNITY5 will look for all its dependencies when the Object is packaged into Assetbundle. This is implemented based on the resource database (Asset). The dependency information is used to determine the Objects collection that will be included into the assetbundle.
The Objects that have been specifically assigned to Assetbundle will be packaged into the Assetbundle only. An object "explicitly assigned" means that the assetbundlename of object is not empty.
If object is not explicitly assigned to a assetbundle, then the object will be included in all assetbundles that reference the object, regardless of whether there is one or more Objects references in the Assetbundle.
If two different Objects are assigned to two different assetbundles, and the two Objects have references to another object, the object will be copied into two assetbundles. Duplicate dependencies are also instantiated, which means that two copies of the dependent Object are considered to be two different Objects and have their own IDs. This will increase the size of the app Assetbundles. If the app loads the two assetbundles, it will cause two copies of the Object to be loaded in memory.
Here are some ways to solve this problem:
- Ensure that the Objects that are packaged into different assetbundles do not have a common dependency. Pack any Objects that have a common dependency into the same assetbundle, avoiding repetitive dependencies.
- This approach does not usually apply to projects that have a lot of co-dependencies. And this method causes the Assetbundles to be frequently rebuilt and re-downloaded as a whole, resulting in inconvenient and less efficient.
- Split the assetbundles so that assetbundles that have a common dependency will not be loaded at the same time.
- This method might work for a particular type of project, such as a level-based game. However, this increases the assetbundles size of the project and increases the number of builds and the number of loads.
- Ensure that all dependent resources are packaged into their own assetbundles. This completely excludes the risk of duplication of resources, but the same can complicate the situation. The application must track dependencies between Assetbundles and ensure that the correct assetbundle has been loaded before any Assetbundle.loadasset methods are called.
In Unity5, the dependency of Object is tracked by the assetdatabase method under the Unityeditor namespace. As the namespace indicates, this method can only be used in the Unity editor, not at run time. The assetdatabase.getdependencies method can be used to locate all direct dependencies on an Object or resource. Note that these dependencies may also be dependent on each other. In addition, theAssetimporter method can be used to query for an Object contained in a assetbundle.
Combining the assetdatabase and assetimporter methods, you can write editor scripts to ensure that a assetbundle all direct or indirect dependencies are packaged into one or the respective Assetbundle, or any of the two assetbundles that have a common dependency, are packaged into the same assetbundle. Considering the memory overhead caused by duplicate resources, it is recommended that all projects have one such script.
Sprite Atlas Repetition (sprite duplication)
When a resource-dependent calculation code in UNITY5 is used in conjunction with an auto-generated Sprite Atlas, there is a singular problem, as described in the following sections.
Any automatically generated sprite Atlas is packaged with the sprite Objects generated by the atlas into the Assetbundle. If the sprite Objects is assigned to multiple assetbundles, then the sprite Atlas is not packaged as a assetbundle, and resource duplication occurs, if the sprite Objects does not indicate ASSETBUNDLE,SP Rite Atlas is also not packaged as a assetbundle.
To ensure that the Sprite Atlas is not duplicated, you need to check that the sprites (same Tag) that is divided into the same sprite Atlas is indicated as the same assetbundle.
(Unity 5.2.2P3 will have additional Atlas issues that have been fixed in unity 5.2.2P4 and are recommended to use as new versions as possible, not to repeat here)
Android Texture Related issues
Because of the serious device fragmentation of the Android ecosystem, it is often necessary to compress textures into different formats. All Android devices support ETC1 format images, however the ETC1 format does not support transparent channels. If the app does not require support for OpenGL ES 2, the simplest way to solve the problem is to use the ETC2 format, which requires OpenGL ES 3 support.
However, most applications need to adapt to older devices, so the ETC2 format may not be available. One way to solve this problem is to use Unity5 's assetbundle version variable. (You can also check out the Unity Android optimization Guide for more details on other solutions)
With the Assetbundle version variable, all textures that are compressed without the ETC1 format must be assigned to assetbundles that contain only textures (texture-only). Next, create the appropriate version variable for the Android ecosystem that does not support the ETC2 format, using a specific texture compression format, such as DXT5, PVRTC, ATITC. For each version variable, change the Textureimporter setting of the texture to match the version variable.
At run time, you can use the Systeminfo.supportstextureformat method to detect supported texture compression formats. You can use this information to select and load the corresponding Assetbundle version variable based on the supported format textures.
For more information on the Android texture compression format, click here to view.
IOS File Handle Overflow
This problem has been solved in Unity 5.3.2p2.
(In a nutshell: iOS generates a file handle when loading a assetbundle, and iOS has a maximum of 255 handles, which will fail to load and error if the limit is exceeded.) Don't repeat it here. )
If this article is helpful to you, please click to praise or comment!
Next Chapter: "Unity3d Technical Document Translation" article 1.9 Unity assetbundle Browse management tool (Final chapter)
"Unity3d Technical Document Translation" article 1.8 Assetbundles Problems and solutions