字体文件并不总是能裁剪,有时候就是需要全量,这种情况下,有个延迟加载会对性能有大帮助
拓展一下,我觉得给图片资源也可以弄个延迟加载的选项
上次看到个帖子,大概是两三年前吧,说是要加一个stream加载的选项,现在也没做
if (MINIGAME && !(TAOBAO || ALIPAY)) {
// @ts-ignore
const downloadTTF = assetManager.downloader._downloaders[".ttf"];
// @ts-ignore
const loadTTF = assetManager.parser._parsers[".ttf"];
assetManager.downloader.register(".ttf", function (url, options, onComplete) {
console.log("download ttf", url);
// 小程序情况下,字体下载后处理,这里直接完成下载
setTimeout(() => {
onComplete(undefined);
}, 1);
});
assetManager.parser.register(".ttf", function (file, options, onComplete) {
console.log("parser ttf");
// 小程序情况下,字体下载后处理,这里直接返回默认字体
onComplete(null, "Arial");
});
TTFFont.prototype.onLoaded = function () {
downloadTTF(this.nativeUrl, {}, (err, data) => {
console.error(err, data);
loadTTF(data, {}, (err, fontFamily) => {
this._nativeAsset = fontFamily;
this.ready = true;
this.emit("ready");
});
});
};
const labelLoad = Label.prototype.onLoad;
Label.prototype.onLoad = function () {
labelLoad.apply(this);
if (this.font instanceof TTFFont && !this.font.ready) {
this.font.once("ready", () => {
this.destroyRenderData();
// @ts-ignore
this._fontAtlas = null;
this.updateRenderData(true);
}, this);
}
};
const labelDestroy = Label.prototype.onDestroy;
Label.prototype.onDestroy = function () {
labelDestroy.apply(this);
if (this.font instanceof TTFFont) {
this.font.targetOff(this);
}
};
}
这样做可以强行弄一下
已建立 issue 进行跟进,后续版本会添加
感恩🥹🥹🥹
6,试一下你这个方案