跳到主要内容

preload 预加载文件

preload

类型: MethodDeclaration

定义位置: loader.ts

描述

预加载文件

返回值

类型: Promise<void>

源代码

位置: 第 42 行

public async preload(): Promise<void> {
const {preload, deployed} = Data.config
if (preload === 'never' ||
preload === 'deployed' && !deployed) {
return
}
// 筛选出需要加载的文件,并统计字节数
const images = []
const audio = []
let totalBytes = 0
for (const {guid, size} of Data.manifest.images) {
if (size !== 0) {
totalBytes += size!
images.push(guid)
}
}
for (const {path, size} of Data.manifest.audio) {
if (size !== 0) {
totalBytes += size!
audio.push(path)
}
}

// 临时修改相关方法
// 等待预加载事件中的文件加载完毕
const {finish} = this
this.finish = () => {
finish.call(this)
this.totalBytes = totalBytes
}
this.finish()
this.updateLoadingStats = Function.empty
EventManager.emit('preload')
await this.promise
// @ts-ignore
delete this.updateLoadingStats

// 开始预加载文件
const promises = []
for (const guid of images) {
promises.push(Loader.loadImage({guid}))
}
for (const path of audio) {
promises.push(Loader.getBlobUrl(path))
}
await Promise.all(promises)
}

文档生成时间:2025/7/7 12:07:06