resources.load 加载 ttf报错

cocos creator 3.5, 字体直接拖到编辑器里面可以正常显示,但是用resources.load接口加载的时候报错。加载代码如下

resources.load(“font/fzcb_font_en”, CC.TTFFont, (err, res) => {
if (err) {
console.error("[UILabel] load font failed path:", fontPath, err.message || err);
return;
}
}

报错如下:
[Scene] [UILabel] load font failed path: font/fzcb_font_en Can not parse this input:{“path”:“font/fzcb_font_en”,“requestType”:“path”,“bundle”:"",“preset”:“default”,“priority”:0}Error: [Scene] [UILabel] load font failed path: font/fzcb_font_en Can not parse this input:{“path”:“font/fzcb_font_en”,“requestType”:“path”,“bundle”:"",“preset”:“default”,“priority”:0}
at file:///E:/workspace/v3/client_v2/temp/programming/packer-driver/targets/editor/chunks/1e/1e71daf8b749dd6d366af8503de70b69392a5c72.js:131:23
at E:\cocos-dashboard\CocosDashboard\resources.editors\Creator\3.5.0\resources\resources\3d\engine\bin.cache\dev\editor\bundled\index.js:93209:7
at E:\cocos-dashboard\CocosDashboard\resources.editors\Creator\3.5.0\resources\resources\3d\engine\bin.cache\dev\editor\bundled\index.js:159243:9
at processTicksAndRejections (internal/process/task_queues.js:75:11)

NewProject_15.zip (146.6 KB)
简单测试正常。

1赞

我是在编辑器模式下报错,正常跑代码是ok的
就是类头部加了这个@_decorator.executeInEditMode(true)

好像跟我之前发现的问题一样。

editor的路径与浏览器里实际运行的路径不一样,需要特殊处理才行
比如resources/test.ttf的路径要转换成db://assets/test.ttf再用Editor.assetdb.remote.urlToUuid()查找uuid然后通过assetManager.loadAny({ type: uuid, uuid: uuid }去加载,这是2.x的做法,3.x没尝试过但原理应该差不多

使用 uuid 加载的方法试了?看下面的回复

现在没有Editor.assetdb.remote.urlToUuid(),拿不到uuid