Exemple #1
0
    // Load All 协程实现
    IEnumerator LoadAllAssetsCoroutine()
    {
        AssetBundleRequest request = this.assetBundle.LoadAllAssetsAsync();

        while (!request.isDone)
        {
            if (loadingAll == null)
            {
                break;
            }
            yield return(false);
        }
        if (loadingAll != null)
        {
            loadingAll.FireCallBack(request.allAssets);
            loadingAll = null;
        }
    }
Exemple #2
0
    // 异步GatAB,允许回调为空
    public static void GetABAsync(string assetbundleName, Action <AssetBundleRef> callback = null)
    {
        AssetBundleRef abRef;

        if (abDict.TryGetValue(assetbundleName, out abRef))
        {
            // 缓存命中
            abRef.Retain();
            if (callback != null)
            {
                callback(abRef);
            }
        }
        else
        {
            AssetLoading <AssetBundleRef> loading;
            // 检查是否已在loading
            if (loadingDict.TryGetValue(assetbundleName, out loading))
            {
                //将本次回调添加到load回调就完事了
                if (callback != null)
                {
                    loading.callback.Add(callback);
                }
                loading.refCount++;
                return;
            }

            loading = new AssetLoading <AssetBundleRef>();
            loadingDict.Add(assetbundleName, loading);
            if (callback != null)
            {
                loading.callback.Add(callback);
            }
            // 异步加载
            AssetBundleLoader.LoadAsync(assetbundleName, (assetbundle) =>
            {
                abRef = new AssetBundleRef(assetbundleName, assetbundle);
                CheckShouldCacheSpriteAsync(abRef, () =>
                {
                    CacheAB(abRef);
                    loadingDict.Remove(assetbundleName);
                    abRef.Retain(loading.refCount - 1);  // ref默认是1,所以只需retain refCount - 1
                    loading.FireCallBack(abRef);
                });
            });
        }
    }
Exemple #3
0
    //TODO 这个方法有些复杂,说明需要简化逻辑
    public override void LoadAsync(GameResType type, string name, Action <UnityEngine.Object> callback, Type resType = null)
    {
        GameBundleInfo data;

        // 检查资源
        if (CheckRes(type, name, out data))
        {
            AssetBundleRef abRef;

            /*
             *  测试发现,异步从ab包加载资源时,即使该assetbundle已被unload(true),仍可以载到奇怪的资源,且能被添加到场景上
             *  而loader被释放可能引起assetbundle被unload
             */
            Action <UnityEngine.Object> checkCallBack = (asset) => //检查异步加载资源结束时,是否该loader已被释放
            {
                if (abrDict == null || loadingDict == null)        // 为null视为已释放
                {
                    callback(null);                                // 直接回调null
                }
                else
                {
                    callback(asset);   // 正常回调上层
                }
            };

            if (abrDict.TryGetValue(data.assetbundle, out abRef))
            {
                abRef.LoadAsync(name, checkCallBack);        // 缓存命中
            }
            else
            {
                // 缓存未命中,则要去加载ab,先声明ab载完回调
                Action <AssetBundleRef> loadABCallback = (assetbundleRef) =>
                {
                    if (assetbundleRef == null)
                    {
                        checkCallBack(null);
                    }
                    else
                    {
                        assetbundleRef.LoadAsync(name, checkCallBack);
                    }
                };

                /*
                 * loadAssetbundle 可以考虑在继承monoLoader的情况下,自己startCoroutine,就不必处理协程结束,而自己已经被销毁的情况
                 */

                AssetLoading <AssetBundleRef> loading;

                // 如果已经在异步load assetbundle
                if (loadingDict.TryGetValue(data.assetbundle, out loading))
                {
                    //将本次回调添加到load回调就完事了
                    loading.callback.Add(loadABCallback);
                    return;
                }

                // 创建一个loading
                loading = new AssetLoading <AssetBundleRef>();
                loadingDict.Add(data.assetbundle, loading);
                loading.callback.Add(loadABCallback);

                // 异步load Assetbundle
                LoadABAsync(data.assetbundle, (assetbundleRef) =>    // 本地缓存未命中,向AssetManager异步请求AssetBundle
                {
                    // 如果loader自身已被释放
                    if (abrDict == null || loadingDict == null)
                    {
                        loading.FireCallBack(null);  // 回调null
                    }
                    // 正常添加到缓存,并回调
                    loadingDict.Remove(data.assetbundle);
                    loading.FireCallBack(assetbundleRef);
                });
            }
        }
        else
        {
            // 没找到这资源
            callback(null);
        }
    }