Unity AssetBundle study notes

Hits: 0

Article directory

Unity AssetBundle

1. Definition and role of AssetBundle

  1. AssetBundle is a compressed package that contains in-game models, textures, prefabs, sound resources and even entire scenes, which can be loaded while the game is running
  2. AssetBundle itself holds dependencies
  3. Put some downloadable content in the AssetBundle to reduce the size of the installation package
  4. Compressed packages can use LZMA and LZ4 compression algorithms to reduce package size and enable faster network transmission

2.What is AssetBundle

A. A compressed package that exists on the hard disk, which contains many files, which can be divided into serialize file and resource file (serialize file and source file)

  1. serialize file: resources are broken into an object, and finally written into a single file. such as prefab
  2. resource file: some binary resources (pictures, sounds) are saved separately for fast loading

B. It is an AssetBundle object that we can load from a specific archive. This object contains the content we added to the compressed package, which can be loaded and used through this object

3. AssetBundle usage flow chart

4. AssetBundle usage process

  1. Specify the AssetBundle property of the resource (ie the generation directory and name)
  2. Build the AssetBundle package
  3. Upload AssetBundle package
  4. Load the AssetBundle package and the resources inside

5. Build the AssetBundle package code

public class CreateAssetBundles
{
    [MenuItem("Assets/Build AssetBundles")]
    static void BuidlAllAssetBundles()
    {
        //Package storage path 
        string dir = "AssetBundles" ;
         //Check if the path exists, otherwise create 
        if (Directory.Exists(dir)== false )
        {
            Directory.CreateDirectory(dir);
        }
        //Build the key API of the AB package, the three parameters are: build path, build method (ie compression method), build platform
        BuildPipeline.BuildAssetBundles(dir, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
    }
}

6. Detailed explanation of the three parameters of BuildPipeline.BuildAssetBundles

  1. Path, as long as it is arbitrary on the hard disk
  2. BuildAssetBundleOptions
    BuildAssetBundleOptions.None: Compressed using the LZMA algorithm, the compressed package is smaller, but the loading time is longer. It needs to be decompressed as a whole before use. Once decompressed, the package is recompressed using LZ4. There is no need to decompress the whole resource when using it. The LZMA algorithm can be used when downloading. Once it is downloaded, it will be saved locally using the LZ4 algorithm. BuildAssetBundleOptions.UncompressedAssetBundle: no compression, large package, fast loading
    BuildAssetBundleOptions.ChunkBasedCompression: using LZ4 compression, the compression rate is not as high as LZMA , but we can load specified resources without unpacking them all.
    Note that with LZ4 compression, you can get loading speeds comparable to uncompressed, and smaller files than uncompressed.
  3. Select the platform to use when Buid comes out

7. Dependency Packaging

8. How to read AssetBundle

  1. AssetBundle.LoadFromMemoryAsync

  2. AssetBundle.LoadFromFile

  3. UnityWebRequest

9. Read the assets in the AssetBundle object

1. General
    T objectFromBundle = bundleObject.LoadAsset<T>(assetName);
2.GameObject
    GameObject gameObject = 
    loadedAssetBundle.LoadAsset<GameObject>(assetName);
3. All resources
    Unity.Object[] objectArray = 
    loadedAssetBundle.LoadAllAssets();

10. Load the Mainfests file to handle resource dependencies

AssetBundle.LoadFromMemoryAsync

AssetBundle assetBundle = AssetBundle.LoadFromFile(manifestFilePath);
AssetBundleManifest manifest = 
assetBundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
string[] dependencies = manifest.GetAllDependencies("assetBundle"); //Pass the name of the bundle you want the dependencies for.
foreach(string dependency in dependencies)
{
    AssetBundle.LoadFromFile(Path.Combine(assetBundlePath, dependency));
}

11.AssetBundle uninstall

AssetBundle.Unload(true) unloads all resources, even if resources are used
(1, called when level switching, scene switching 2, when resources are not used)
AssetBundle.Unload(false) Unloads all unused resources

How to unload individual resources 1, through Resources.UnloadUnusedAssets. 2, when the scene is switched

12. File verification

Similarities
between CRC MD5 and SHA1:
CRC, MD5, and SHA1 all generate a check value by calculating the data, which is used to check the integrity of the data.
difference:

  1. Algorithms are different. CRC uses polynomial division, and MD5 and SHA1 use methods such as replacement and rotation;
  2. The length of the check value is different. The length of the CRC check bit is related to its polynomial, generally 16 bits or 32 bits; MD5 is 16 bytes (128 bits); SHA1 is 20 bytes (160 bits);
  3. The name of the check value is different. CRC is generally called CRC value; MD5 and SHA1 are generally called hash value (Hash) or hash value;
  4. Security is different. The security here refers to the ability of error detection, that is, the error of the data can be detected by the check bit. The security of CRC has a lot to do with polynomials, and is much weaker than MD5 and SHA1; MD5 is very secure, but it was cracked by Wang Xiaoyun of Shandong University in 2004; SHA1 has the highest security.
  5. The efficiency is different, the calculation efficiency of CRC is very high; MD5 and SHA1 are slower.
  6. Different uses. CRC is generally used for the verification of communication data; MD5 and SHA1 are used in the field of security, such as file verification and digital signatures.

You may also like...

Leave a Reply

Your email address will not be published.