# Resource loading

Game resources such as textures, materials, and models are a series of static files in a game project; when the game is running, they are instantiated into a series of resource instances, which are dependent on components such as UISprite and MeshRender to participate in rendering.

The resource system does two things:

  1. When the game project is built, package the resource files.
  2. When the game is running, load the resource file and create a resource instance.

Let's first understand how resources are loaded. First, you need to configure the resource you want to load in the project window as an entry resource through the import setting under the inspector. Developers do not need to deal with dependencies in the code, and do not need to deal with the reading of resource files. They only need to call Loader.prototype.load() to obtain the loaded and instantiated resources. Loader is the loader Class used to load resources. There are only two core APIs of the resource system and Loader's core APIs:

-Loader.prototype.load() is used to load resources asynchronously. -Loader.prototype.getAsset() is used to obtain downloaded resources synchronously.

Loader.prototype.load() will return a LoadTask, you can monitor the result of the load task, or interrupt it. The first parameter of Loader.load() is the path of the resource relative to the game root directory/assets/.

Assuming the absolute path of SpriteFrame is game root directory/assets/Assets/image.spriteframe, then its path relative to game root directory/assets/ is Assets/image.spriteframe.

// Load a prefab
const task = engine.loader.load("resource/Aircraft.prefab");
task.promise.then((prefab) => {
  console.log(prefab)
}).catch((error) => {
  console.error(error);
})
// Interrupt the task, the catch of task.promise will be executed
task.abort();

After the resource is loaded successfully, if you want to obtain the resource instance by synchronous calling, you can call Loader.prototype.getAsset().

// If it has not been loaded and the loading has not been completed, an error will be thrown
try {
  const spriteFrame = engine.loader.getAsset("resource/Aircraft.prefab");
}
catch (e) {
  console.error(e)
}

# LoadTask

What engine.loader.load returns is a LoadTask instance, and LoadTask is a download task class returned to the business layer, which is a "one-off" class. From instantiation to the completion of the loading of all dependent resource packages (success or failure), the state does not change.

For example, you can use LoadTask.progress to get the progress, and abort() to terminate the task.

# Additional options for the loader

When loading resources using Loader.load(), you can pass in additional download options in the second parameter.

Common options Type Description
httpRetryCount number The number of retries after a failed download.
httpPriority number Download priority.
preload boolean preload, do not read the file after downloading.
raw boolean Load the contents of the original file.
engine.loader.load("resource/Aircraft.prefab", {
  httpRetryCount: 2,
  priority: 10
}).promise.then((spriteFrame) => {
  console.log(spriteFrame)
})

# Number of global retries

If you don't want to pass additional parameters every time load(), you can directly set the number of global retries in the global configuration.

Open the Project Setting tab in the tool >> Game Configuration >> globalSetting >> globalHTTPRetry

# Resource download optimization

In order to allow users to enter the game faster, developers can also optimize their game logic in the following ways.

Early download: For users who enter the game for the first time, they will generally enter the loading interface first, then you can start downloading game resources in the loading interface in advance. The download will run in a multi-threaded worker and will not cause any damage to the main process. Multi-impact.

Download on demand: There is no need to download the game resources that users don’t need immediately. You can download them when you need them. Developers need to optimize them for their own scenarios.

Bake streaming scenes: Small games realize the ability to download scene resources step by step by distance for large scenes, realizing the ability to open super-large maps in seconds. You can check document for details.

点击咨询小助手