加载远程资源

This commit is contained in:
chen wei bo
2025-09-16 11:19:58 +08:00
parent f0fbf83239
commit 31523af745
3 changed files with 70 additions and 21 deletions
+37 -14
View File
@@ -329,17 +329,16 @@ export default class ResManager {
// })
// }
//region 加载远程图片
/**
* 获取远程图片资源并设置
* @param spt 要设置的节点
* @param url url地址
* @param suffix 图片后缀名
* @param cb 回调函数
*/
changeRemoteSpriteFrame(spt: Sprite, url: string, suffix: string = 'png', cb: Function = null) {
/** 加载远程图片 */
changeRemoteSpriteFrame(spt: Sprite, url: string, cb: Function = null) {
if (!spt || !url) return;
assetManager.loadRemote<ImageAsset>(url, {ext: '.'+suffix}, (err, res: ImageAsset) => {
// 临时加了一个字段,用来保存当前加载的资源路径
let spttemp:any = spt;
// 在异步回调时判断这个组件是不是还在加载同一个 url,避免多次调用时产生覆盖冲突
spttemp.frameUrl = url;
// 加载
assetManager.loadRemote<ImageAsset>(url, (err, res: ImageAsset) => {
console.log("加载远程图片: ", url);
if (err) {
console.log(err);
return
@@ -347,18 +346,42 @@ export default class ResManager {
if (!spt.isValid) {
return;
}
if (spttemp.frameUrl != url) {
return;
}
const spriteFrame = new SpriteFrame();
const texture = new Texture2D();
texture.image = res;
spriteFrame.texture = texture;
spt.spriteFrame = spriteFrame;
cb && cb(res);
})
}
}
/** 加载远程视频 */
loadRemoteVideo(spt: VideoPlayer, url: string, cb: Function = null) {
if (!spt || !url) return;
// 临时加了一个字段,用来保存当前加载的资源路径
let spttemp:any = spt;
// 在异步回调时判断这个组件是不是还在加载同一个 url,避免多次调用时产生覆盖冲突
spttemp.frameUrl = url;
// 加载
assetManager.loadRemote<VideoClip>(url, (err, res: VideoClip) => {
console.log("加载远程视频: ", url);
if (err) {
console.error(err);
return;
}
if (!spt.isValid) {
return;
}
if (spttemp.frameUrl != url) {
return;
}
spt.clip = res;
cb && cb(res);
})
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/** 加载resources里的PB path:不带后缀*/