Ejemplo n.º 1
0
    /// <summary>
    /// 从缓存链中获取一个指定名称的缓存池
    /// </summary>
    /// <param name="poolName">池的名称</param>
    /// <param name="cacheCount">缓存池的大小</param>
    /// <returns>如果缓存链中不存在,则默认会创建</returns>
    private ARCCache <string, Asset> swapnCachePool(string poolName, int cacheCount = 10)
    {
        if (bundlePoolCache.ContainsKey(poolName))
        {
            return(bundlePoolCache[poolName]);
        }

        ARCCache <string, Asset> cache = new ARCCache <string, Asset>(cacheCount);

        //释放清理
        cache.DestroyCallback = bundle =>
        {
            bundle.Value.Unload(false);
            Resources.UnloadUnusedAssets();
            System.GC.Collect();
        };
        bundlePoolCache[poolName] = cache;
        return(cache);
    }
Ejemplo n.º 2
0
    /// <summary>
    /// 加载AssetBundle及其依赖的资源
    /// </summary>
    /// <param name="cachePool">缓存池</param>
    /// <param name="bundleName">目标AssetBundle资源</param>
    /// <param name="callback">最终的资源加载完成时的回调</param>
    /// <returns></returns>
    private IEnumerator loadBundleAndDependenices(ARCCache <string, Asset> cachePool, string bundleName,
                                                  Action <AssetBundle> callback, Action <float, float> progress)
    {
        string[] depAssetArr = manifest.GetAllDependencies(bundleName);
        foreach (string depAsset in depAssetArr)
        {
            //重新获取Bundle名,因为可能依赖的名称内带有目录信息
            string depBundleName = Path.GetFileName(depAsset);
            yield return(AppInter.StartCoroutine(AsyncLoadBundleAtPath(depBundleName, (obj) =>
            {
                if (obj != null)
                {
                    Asset asset = new Asset(depBundleName, obj);
                    cachePool.Put(depBundleName, asset);
                }
            })));
        }

        yield return(AppInter.StartCoroutine(AsyncLoadBundleAtPath(bundleName, callback, progress)));
    }
Ejemplo n.º 3
0
    //    /// <summary>
    //    /// 从缓存中获取资源
    //    /// </summary>
    //    /// <param name="poolName">缓存池的名称</param>
    //    /// <param name="path">资源相对路径</param>
    //    /// <param name="callback">加载完成的回调</param>
    //    public void SwapAssetAtPath(string poolName , string path , Action<UnityEngine.Object> callback)
    //    {
    //        ARCCache<string, AssetBundle> cachePool = swapnCachePool(poolName);
    //        AssetBundle bundle = cachePool.Get(path);
    //        if (bundle != null)
    //        {
    //            if (callback != null)
    //                callback(bundle.mainAsset);
    //            return;
    //        }
    //
    //        //异步加载
    //        AppInter.StartCoroutine(AsyncLoadAtPath(path,EAssetBaseType.AssetBundle, (obj) =>
    //        {
    //            bundle = obj as AssetBundle;
    //            if (bundle == null)
    //            {
    //                Debugger.LogWarning("<<AssetLoader , SwapAsset>> load faile !  Path -->> " + path);
    //                return;
    //            }
    //            cachePool.Put(path , bundle);
    //
    //            if(callback != null)
    //                callback(bundle.mainAsset);
    //        }));
    //    }

    /// <summary>
    /// 从AssetBundle缓存中获取资源
    /// </summary>
    /// <param name="poolName">缓存池的名称</param>
    /// <param name="resName">资源名</param>
    /// <param name="callback">加载完成的回调</param>
    public IEnumerator SwapAssetByName <T>(string poolName, string resName, EAssetType assetType,
                                           Action <UnityEngine.Object> callback, Action <float, float> progress)
        where T : UnityEngine.Object
    {
        ARCCache <string, Asset> cachePool = swapnCachePool(poolName);
        string bundleName = GetBundleName(resName, assetType);

        if (string.IsNullOrEmpty(bundleName))
        {
            callback.Invoke(null);
            Debugger.LogWarning("<<AssetLoader , SwapAssetByName>> load faile !  Res -->> " + resName);
            yield break;
        }

        Asset bundle = cachePool.Get(bundleName);

#if UNITY_EDITOR
        if (!isBundle)
        {
            if (bundle == null)
            {
                string             path = GetBundleEncrypeName(resName + assetType.ToString().ToLower());
                UnityEngine.Object obj  = AssetDatabase.LoadAssetAtPath <T>(path);
                if (obj == null)
                {
                    Debugger.LogWarning("<<AssetLoader , SwapAssetByName>> load faile !  Path -->> " + path);
                    callback.Invoke(obj);
                    yield break;
                }

                bundle = new Asset(bundleName, obj);
                cachePool.Put(bundleName, bundle);
            }
            callback.Invoke(bundle.Resource);
            yield break;
        }
#endif

        if (bundle != null)
        {
            if (callback != null)
            {
                callback(bundle.Get <AssetBundle>().LoadAsset <T>(resName));
            }
            yield break;
        }
        //异步加载
        yield return(AppInter.StartCoroutine(loadBundleAndDependenices(cachePool, bundleName, (obj) =>
        {
            if (obj == null)
            {
                Debugger.LogWarning("<<AssetLoader , SwapAssetByName>> load faile !  Path -->> " + bundleName);
                callback.Invoke(null);
                return;
            }

            Asset asset = new Asset(bundleName, obj);
            cachePool.Put(bundleName, asset);

            if (callback != null)
            {
                callback(obj.LoadAsset <T>(resName));
            }
        }, progress)));
    }
Ejemplo n.º 4
0
 private IEnumerator loadBundleAndDependenices(ARCCache <string, Asset> cachePool, string bundleName,
                                               Action <AssetBundle> callback)
 {
     return(loadBundleAndDependenices(cachePool, bundleName, callback, null));
 }