cocos creator 3.3 bundle资源加载,从缓存中取

cocos creator 3.3 加载bundle本地已经有了,每次还是会发送http请求,状态码是304也就是资源状态没有变化。加载资源还是比较慢?为什么用了缓存中的资源,还要http请求。
是用的不对吗?

我是直接从bundle.load资源,然后再次进入的时候还是回去加载,第一次下载可以理解,第二次还是发送了 http请求,内网,速度没有快多少。

贴下你的加载代码,是加载什么资源了?我们确认下。

public async preloadBundlePrefab(formName:string){

    let data = this.splitFormName(formName);

    let bundleName = data.bundle;

    let prefabName = data.prefabName;

    if(this.loadPrefabWithBundleKey.has(bundleName) && this.loadPrefabWithBundleKey.get(bundleName)?.has(formName)){

        this.loadProgress[formName] = 1;

        this.calProgress(2);

        return;

    }

    let bundle = this.subGameBundle.get(bundleName);

    if (bundle == undefined){

        console.error(`${bundleName}不存在`);

        return;

    }

    if (!this.loadPrefabWithBundleKey.has(bundleName)){

        this.loadPrefabWithBundleKey.set(bundleName,new Map<string,Prefab>());

    }

    bundle!.load<Prefab>(prefabName,(loadCnt:number,allCnt:number,item:AssetManager.RequestItem)=>{

        this.loadProgress[formName] = loadCnt/allCnt;

        this.calProgress(0);

    },(err,prefab:Prefab)=>{

        if (err != null){

            console.log(err);

            console.warn(`${formName}加载失败`);

        }else{

            console.log(`${formName}加载成功`);

            this.loadProgress[formName] = 1;

            if (!this.loadPrefabWithBundleKey.has(bundleName)){

                this.loadPrefabWithBundleKey.set(bundleName,new Map<string,Prefab>());

            }

            this.loadPrefabWithBundleKey.get(bundleName)!.set(formName,prefab);

            this.loadPrefabWithBundleKey.get(bundleName)!.get(formName)!.addRef();

            this.calProgress(1);

        }

    });

}

这是加载prefab的代码。在此之前已经加载过bundle 了。然后再次加载还是会有 去http请求,只是状态码是 304

首次请求的话 状态码是 200

这个问题是怎么回事?是我用的有问题吗?

可以私信给我个demo吗?这个正常是会从缓存加载的,需要排查下。

好。我现在去准备。
正常情况下从缓存中去,是不是就不会发送http请求了?
另外浏览器是不是有什么设置?

public releaseAllResByList(list:Map<string,number>){

    let bName = "";

    for (let key of list.keys()){

        let resData = this.splitFormName(key);

        let bundleName = resData.bundle;

        bName =bundleName;

        if(list.get(key) == ResType.ResType_SpriteFrame){

            if (!this.loadSpriteFrameWithBundleKey.has(bundleName)){

                continue;

            }

            

            if (!this.loadSpriteFrameWithBundleKey.get(bName)!.has(key)){

                continue;

            }

            while(this.loadSpriteFrameWithBundleKey.get(bName)!.get(key)!.refCount >0) {

                this.loadSpriteFrameWithBundleKey.get(bName)!.get(key)!.decRef();

            }

            this.loadSpriteFrameWithBundleKey.get(bName)!.delete(key);

        }else if(list.get(key) == ResType.ResType_SpriteAtlas){

            //释放图集资源

            if(!this.loadSpriteAtlasWithBundleKey.has(bundleName)){

                continue;

            }

            if(!this.loadSpriteAtlasWithBundleKey.get(bName)!.has(key)){

                continue;

            }

            while(this.loadSpriteAtlasWithBundleKey.get(bName)!.get(key)!.refCount >0) {

                this.loadSpriteAtlasWithBundleKey.get(bName)!.get(key)!.decRef();

            }

            this.loadSpriteAtlasWithBundleKey.get(bName)!.delete(key);

        }else if(list.get(key) == ResType.ResType_Prefab){

            //预制体资源

            if(!this.loadPrefabWithBundleKey.has(bundleName)){

                continue;

            }

            if(!this.loadPrefabWithBundleKey.get(bName)!.has(key)){

                continue;

            }

            while(this.loadPrefabWithBundleKey.get(bName)!.get(key)!.refCount >0) {

                this.loadPrefabWithBundleKey.get(bName)!.get(key)!.decRef();

            }

            this.loadPrefabWithBundleKey.get(bName)!.delete(key);

        }else if(list.get(key) == ResType.ResType_AudioClip){

            //音频资源

            if(!this.loadAudioClipWithBundleKey.has(bundleName)){

                continue;

            }

            bName = bundleName;

            if(!this.loadAudioClipWithBundleKey.get(bName)!.has(key)){

                continue;

            }

            while(this.loadAudioClipWithBundleKey.get(bName)!.get(key)!.refCount >0) {

                this.loadAudioClipWithBundleKey.get(bName)!.get(key)!.decRef();

            }

            this.loadAudioClipWithBundleKey.get(bName)!.delete(key);

        }

    }

    if (bName != ""  ) {

        this.loadPrefabWithBundleKey.delete(bName);

        this.loadAudioClipWithBundleKey.delete(bName);

        this.loadSpriteFrameWithBundleKey.delete(bName);

        this.loadSpriteAtlasWithBundleKey.delete(bName);

        // if(this.subGameBundle.has(bName)){

        //     this.subGameBundle.delete(bName);

        // }

        //调用了 releaseAll 应该不需要再 desRef() 方法了。源码中tryRelease就是将

        //这里releaaseAll还不能调用,如果调用,第二次加载sprite的时候还是有问题的。 暂时不调用,但是回头要处理一下

        //需要调用releaseAll否则有的资源释放不掉

        //assetManager.getBundle(bName)!.releaseAll();

        //assetManager.removeBundle(assetManager.getBundle(bName)!);

        console.log("releaseAll执行了-----",bName);

    }

上面是我加载bundle和离开bundle的时候释放资源的方法。你先看一下有没有问题。没有removeBundle。但是里边的资源释放了。

如果你释放了资源,可能会触发重新下载不存在的资源,304 是从浏览器本地缓存加载资源,只是减少网络传输的时间损耗,资源解析正常执行,实际还是要看你的资源使用情况。