-
Creator 版本:3.8.8
-
目标平台:抖音小游戏
问题 就是 我的游戏 资源是 远程的Bundle 挂在 阿里云oss存储里面 每次游戏时候 本地缓存调试 都有缓存进去 但是 就是 关闭游戏 在开启 游戏资源又从远程一下来 相当费流量 也查很多资料 也给每个资源设置了 Cache-Control:max-age=604800 频繁下载流量太大了 能否有高人帮我分析一下 怎么改这个问题
private static loadFromHotBundle(
path: string,
isBuffer: boolean,
type: any,
bundleName: string,
callback: (asset: Asset | null) => void
): void {
const assetType = isBuffer ? BufferAsset : type;
// // ✅ 抖音 / 微信 / 支付宝小游戏:禁止 bundle
// if (PlatformUtil.isAnyMiniGame()) {
// const remoteUrl = `${PlatformAPI.getCDNRoot()}/${bundleName}/${path}`;
// // console.log(`[MiniGame] loadRemote: ${remoteUrl}`);
// assetManager.loadRemote(remoteUrl, assetType, (err, asset) => {
// if (err || !asset) {
// console.warn(`[MiniGame] loadRemote failed: ${remoteUrl}`);
// callback(null);
// return;
// }
// console.log(`[MiniGame] loadRemote ok : ${remoteUrl}`);
// callback(asset);
// });
// return;
// }
if (!PlatformUtil.isByteDanceMiniGame()) { //不是小游戏 走这个
callback(null);
return;
}
const loadAsset = (bundle: any) => {
if (!bundle) {
callback(null);
return;
}
const realPath = this.resolveHotBundlePath(bundle, path);
console.log(`[HotBundle] load: ${realPath}`);
bundle.load(realPath, assetType, (err, asset) => {
if (err || !asset) {
console.log(`[HotBundle] miss -> fallback: ${path}`);
callback(null);
return;
}
// 加密资源处理
// if (isBuffer) {
// this.decryptAndCache(path, asset);
// asset = this.loadedResources[`${path}#buffer`];
// }
// console.log("最终路径:", asset.nativeUrl);
const cachePath = assetManager.cacheManager.getCache(asset.nativeUrl);
console.log('缓存路径:', cachePath);
// if (PlatformAPI.isCachedLocally(asset)) {
// console.log(`[checkCache] ${path} 已在本地缓存 ✅`, asset.nativeUrl);
// } else {
// console.log(`[checkCache] ${path} 仍然走远程 ❌`, asset.nativeUrl);
// }
callback(asset);
});
};
const cachedBundle = this.hotBundles.get(bundleName);
// Bundle 已存在
// if (this.hotBundle && bundleName =="hotbundle")
if (cachedBundle) {
loadAsset(cachedBundle);
return;
}
// spine Bundle 已存在
// if (this.hotSpineBundle && bundleName =="hotspine") {
// loadAsset(this.hotSpineBundle);
// return;
// }
// let updateData:any = PlatformAPI.getUpdateData();
// const hotPath = updateData.hotbundleUrl;
let hotPath = bundleName;
if (PlatformUtil.isAnyMiniGame()) {
hotPath =`${PlatformAPI.getCDNRoot()}/${bundleName}`;
}else{
hotPath = native.fileUtils.getWritablePath() + bundleName;//'hotbundle';
if (!native.fileUtils.isDirectoryExist(hotPath)) {
callback(null);
return;
}
}
console.log("获得hotBundle"+hotPath);
assetManager.loadBundle(hotPath,{
cacheEnabled: true // 确保缓存开启
}, (err, bundle) => {
if (err || !bundle) {
console.warn('[HotBundle] loadBundle failed');
callback(null);
return;
}
if (PlatformUtil.isAnyMiniGame()) {
console.log('[isAnyMiniGame] bundle loaded');
}else{
console.log('[HotBundle] bundle loaded');
}
this.hotBundles.set(bundleName, bundle);
loadAsset(bundle);
});
}
这个是我 一段加载远程的 代码 游戏流程都是没问题 就是流量一直控制不住 在抖音小游戏的工具里面也看到 缓存