AssetBundle_AB packaging process and basic usage

AssetBundle

AssetBundle is simply called AB package. It is a compressed package of asset files. It is somewhat similar to a compressed file. AB package can be loaded dynamically. We can package the resources in the game into AB package. When we need something Resources are obtained from the AB package, which saves space and further reduces the size of the game package. Therefore, the AB package can also be used as a hot update in the game. When the game needs to release new content, the new AB is directly loaded. without having to re-publish the entire game

The function of AB package

  1. When it comes to AB packages, you have to think of Resources. AB packages have more advantages than Resources.
    • All files in the Resources directory will be packaged together when packaging, and this directory is read-only and cannot be modified.
    • The storage location of the AB package can be customized, the compression method can be customized, and it can be modified later (for hot updates)
  2. Reduce the package size, compress resources, and reduce the initial package size (for example, a game may require 2 G to download, which consumes more traffic. If some resources are used as AB packages in the form of AB packages, the initial package size may It’s only a few hundred MB, so you can download it online when you enter the game later.
  3. Resource hot update, script hot update

Use Unity’s official packaging tool AssetBundle Browser for packaging

  • Why can't I package C# script resources?
    • First of all, C# is a compiled language. Before running the C# code logic, it needs to be translated into machine language by the compiler. The program generated after C# compilation cannot be modified at runtime and can only take effect after recompilation.

    • Lua is an interpreted language that can dynamically modify the code at runtime without recompiling. Since Lua does not need to be compiled, when there is new game logic, you only need to replace the corresponding Lua script file or update it. Just Lua code
      Insert image description here

  • Build parameter explanation
    Insert image description here
    Insert image description here
  • What does the packaged AB package file contain?
    Insert image description here

Loading AssetBundle

Copy the StreamingAsset folder. Because the output paths of different platforms are different, the streaming folder is readable and the path is unified by default.

Load the main package, load the main package first before loading the AB package

Loading process

  • Synchronously load AB package

    • Load ab package
    AssetBundle ab = AssetBundle.LoadFromFile(填写ab包路径)
    
    • When loading resources in the ab package, it is best to use generics to avoid conflicts when loading resources of different types with the same name.
    ab.LoadAsset<GameObject>("Cube");
    
  • Note: AB package cannot be loaded repeatedly, otherwise an error will be reported

  • Asynchronously load AB package

    • Asynchronous loading ⇒ coroutine
    StartCoroutine(LoadABRes("ui", "Bonus_02"));
    
    IEnumerator LoadABRes(string ABName, string resName)//AB包名 资源名
    {
          
          
        //第一步 异步加载AB包
        AssetBundleCreateRequest UI = AssetBundle.LoadFromFileAsync(Application.streamingAssetsPath + "/" + ABName);
        yield return UI;
        //第二步 异步加载资源
        AssetBundleRequest cursor =                             UI.assetBundle.LoadAssetAsync(resName, typeof(Sprite));
        yield return cursor;
        img.sprite = (Sprite)cursor.asset;
    }
    
  • Uninstall AB package

    • false means only unloading ab packages that are not loaded, true means unloading all (including objects in the scene that have been loaded)
    AssetBundle.UnloadAllAssetBundles(false);
    
    • To uninstall a single ab package, just uninstall it yourself.
    AssetBundle ab = AssetBundle.LoadFromFile(填写ab包路径)
    ab.Unload(false);
    
AB package dependencies
  • What are the dependencies of AB package?
    If a resource uses other AB package resources, at this time, if only its own AB package is loaded, then the resources created through this package Objects will experience resource loss because they do not belong to the same package. You only need to load the dependent packages together and they will display normally

  • Therefore, if there is a dependency between two packages, both packages need to be loaded (both the package itself and the dependent package need to be loaded)

  • How to load dependent packages

    • Use the main package to obtain dependencies. The main package is the ab package with the same name as the output path. The dependency information in all packages is recorded in the main package.
    1. Load main package
    AssetBundle abMain = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + "AB_Path");
    
    1. Load fixed configuration file (mainfest), fixed writing method, direct memory
    AssetBundleManifest abMainfest = abMain.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
    
    1. Get the dependency package information of the specified package from the fixed configuration file, and then load these dependency packages so that there will be no resource null references
      Return through GetAllDependencies() All dependent package information of the specified package, including the package name string string of these dependent packages
    string[] strs = abMainfest.GetAllDependencies("model");
    

    Load all these dependent packages through the dependent package names returned by the main package.

    for (int i = 0; i < strs.Length; i++)
        {
          
          
            AssetBundle ab2 = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + strs[i]);
        }
    

Guess you like

Origin blog.csdn.net/m0_69778537/article/details/134588145
Recommended