With the increasingly rich gameplay of small games, developers are increasingly demanding to expand the package size, so wechat launched a function of subcontracting and loading small games.

The so-called subcontracting loading means that the game content is divided into several packages according to certain rules, and the necessary package is downloaded at the first startup, which is called the “main package”. The developer can trigger the downloads of other subpackages in the main package, so as to spread the time of the first download into the game operation.

Subcontract load package size limit

Currently, the subcontracting size of small games has the following limitations:

  • The size of all packages is less than 8M
  • The size of a single subcontract/main package should not exceed 4M

The subcontract configuration

You need to configure the subcontracting information in game.json. Suppose the game’s directory structure looks like this:

|----game.js
|----game.json
|----images
|    |----a.png
|    |----b.png
|----stage1
|    |----game.js
|    |----images
|         |----1.png
|         |----2.png
|----stage2.js
Copy the code

We use the stage1 folder as a subcontractor and stage2.js as a subcontractor, so the configuration in game.json is:

{' subpackages' : [{' name ':' stage1 ', 'root' : 'stage1'}, {' name ': "Stage2", "root" : "stage2.js" // can also specify a js file}]}Copy the code

Directories or JS files configured in subpackages will be packaged as subpackages. Directories or JS files not configured in subpackages will be packaged into the main package.

The subcontract to load

Wechat provides wx.loadSubPackage () API to trigger the download of subcontract. After calling wx.loadSubPackage, the download and load of subcontract will be triggered.

After the load is complete, the completion of the load is notified by the SUCCESS function of wx.loadSubpackage.

At the same time, wx.loadSubpackage returns a LoadSubpackageTask, which can be used to obtain the current download progress.

Sample code:

const loadTask = wx.loadSubpackage({
    name: 'stage1'// name can be filled with name or root success:function(res){// Pass the SUCCESS callback after the subcontract load is complete}, fail:function(res) {/ / subcontract loading failure through fail callback}}) loadTask. OnProgressUpdate (res = > {the console. The log ('Download speed',res.progress);
    console.log('Length of data downloaded',res.totalBytesWritten);
    console.log('Total length of data expected to be downloaded', res.totalBytesExpectedToWrite)
})
Copy the code

Compatible with older versions

Wechat background compilation deals with the compatibility of the client of the old version, and the background will compile two code packages, one is the subcontracted code, the other is the compatible code of the whole package. For older clients, download the entire package of code to start.

Developers in base libraries below 2.1.0 do not need to call wx.loadSubpackage to trigger loading because the wx.loadSubpackage method does not exist in base libraries below 2.1.0.

In older versions, the developer needed to call require to trigger the loading of the subcontract entry file. Such as:

Require (" stage1 / game. Js')Copy the code

If you do not intend to support older versions, developers can block users of the base version below 2.1.0 through the MP mini game background configuration.

A known BUG

Currently Android cannot load font files under subcontract, this is expected to be fixed in the next client release.

This section will introduce you here, and the next section will introduce you to the relevant content of small game audio playback.

Learning is a path that sometimes makes people cry for joy and sometimes depressed. If you feel this article is helpful to you, please appreciate it as appropriate!