Bundle of cocosCreator (2)

Version: v3.4.0

Reference: Asset Bundle

Introduction

Regarding Bundle, it can be understood as resource modularization, which allows developers to divide resources such as textures and scripts into multiple different Bundles according to project requirements.

During the running of the project, load different Bundles according to the requirements, so as to reduce the number of resources loaded at startup, etc.

Through BuiltinBundleName under AssetManager, you can know:

export namespace AssetManager {<!-- -->
  // built-in bundle
  export enum BuiltinBundleName {<!-- -->
    RESOURCES = "resources",
    MAIN = "main",
    START_SCENE = "start-scene"
  }
}

The Bundle built into the engine mainly includes:

  • main stores all the scenes and dependent resources checked in Participate in the construction scene of the build release panel, and use the main package compression type< /strong>or Configure the main package to be generated as a remote package.
  • resources Generate resources folder under assets directory by configuring Resource Manager. For more information, please refer to: resources of cocosCreator (1)
  • start-scene Check Initial Scene Subpackage on the Build Release panel, then the first scene will be built to start- scene

The built-in Bundle will be generated when it is packaged and built after it is set in the build release. Take main and resources as an example:

Please add a picture description

After the build, it will be generated in the directory of build’s android-001:

Please add a picture description

The directory structure of Bundle is similar, the main content:

  • Code The code related will generate an entry script file of index.js or game.js according to the platform
  • Resources Resources and dependent resources will be placed in the import or native directory
  • Resource Configuration Resource configuration information is placed under config.json

Configuration

Bundle In addition to the official built-in, it also supports custom configuration. Its configuration is based on folder. When we select a folder in the Explorer, the Property Inspector will An option to Configure as Bundle appears.

Please add a picture description

After checking the Configure as Bundle option
Please add a picture description

The official gave a good hint, its configuration parameters:

Configuration parameters Description
Bundle name build name, the folder name is used by default, you can modify it as needed
Bundle priority build from large to small< Build in the order of /strong>, and the priority setting can be the same, but mainly try to ensure the shared resources Texture, SpriteFrame, Audio, etc. Bundle has a higher priority
Target platform Different platforms can use different configurations, and the Bundle is generated according to the platform during construction
Compression type Different platforms have different compression types
Configure as remote package if If checked, it will be placed in the remote folder after construction, and the content needs to be placed on the remote server

For the compression type, there are mainly the following types:

Compression Type Description
No Compression There is no compression operation
Merge dependencies When building a Bundle, it will depend on each other The JSON files of resources are merged together to reduce the number of loading requests at runtime
Merge all JSON When building a Bundle, All JSON resources will be merged into one, which can minimize the number of requests on the web platform, but will increase the loading time of a single resource. It is not recommended to use on the Android platform, it will increase the package size of the hot update
Small game subpackage In the small game platform that provides the subpackage function, the Bundle will be set as the subpackage on the corresponding platform
ZIP On some small game platforms, building a Bundle will compress resource files into a Zip file to reduce the number of loading requests at runtime

Regarding priority, the levels of the built-in Bundle are:

Asset Bundle Priority
main 7
resources 8
start-scene 20

Note: The custom Bundle should not have higher priority than the built-in Bundle, so as not to share the resources of the built-in Bundle.

In addition, Script also supports Bundle. If Bundle is set, all script files will be merged into one .js file, which will be loaded when Bundle is loaded This js file.

For example, taking assets/scripts as an example, after setting up the Bundle, publish build through android build:

Please add a picture description

The index.js file contains all the content related to the script file.

Script loading

Bundle is used in the script, mainly through cc.assetManager, its main methods are:

export const assetManager: AssetManager;
export class AssetManager {<!-- -->
  // Get the loaded bundle collection
  bundles: __private.cocos_core_asset_manager_cache_ICache<AssetManager.Bundle>;
  // Get the built-in main package
  get main(): AssetManager. Bundle | null;
  // Get the built-in resources package
  get resources(): AssetManager. Bundle | null;
  // Get loaded subpackages
  getBundle(name: string): AssetManager. Bundle | null;
  // Remove the specified bundle package
  removeBundle(bundle: AssetManager. Bundle): void;
  // load package
  loadBundle(name, options, onComplete);
}

Simple example:

// load script Bundle
assetManager.loadBundle("DemoBundle", (err: Error, data) => {<!-- -->
  console.log("loadBundle data:", data);
});

// Get the loaded bundle
let scriptBundle = assetManager. getBundle("DemoBundle");
console.log("loaded scriptBundle:", scriptBundle);

Notice:

  • Except for the built-in main and resources Bundles, other custom Bundles must be loaded before they can pass getBundle code> get
  • For scripts, it is best not to refer to each other, which may cause the script to not be found at runtime

For the use of Bundle, it is probably similar to the use of resources. The specific methods are:

  • load load resources
  • loadDir load resources in batches
  • preload preload resources
  • preloadDir preload resources in the directory

For more information, please refer to the resources (1) of the previous blog cocosCreator, a simple example:

protected onLoad(): void {<!-- -->
  // load Bundle
  assetManager.loadBundle("DemoBundle", (err: Error, bundle) => {<!-- -->
    if (err) {<!-- -->
      return console. error(err);
    }
    this.initBundle(bundle);
  });
}

private initBundle(bundle: any) {<!-- -->
  console.log("bundle name:", bundle.name);
  console.log("bundle root path:", bundle.base);
  console. log(bundle);

  // preload bundle resources
  bundle.preload("sound/click", (err, data) => {<!-- -->
    if (err) {<!-- -->
      return console. error(err);
    }
    console.log("localConfig data:", data);
  });

  // Preload directory resources
  bundle.preloadDir("spine", (err, data) => {<!-- -->
    if (err) {<!-- -->
      return console. error(err);
    }
  })

  // load resources
  bundle.load("sound/click", (err, data) => {<!-- -->
    if (err) {<!-- -->
      return console. error(err);
    }
    console. log(data);
  });
}

syntaxbug.com © 2021 All Rights Reserved.