Пример #1
0
        IEnumerator Load_AssetBundle_Single(string ab_path, Action <AssetBundle> callback, bool loadFormPerPath, string[] dependences)
        {
            //Debug.Log("协程 收到 单独AB包加载子请求:" + ab_path);
            //加载AB本体
            //求当前ab的实际加载路径(绝对路径)
            string ab_full_path;

            if (loadFormPerPath)
            {
                ab_full_path = mPersistentDataPath + ab_path;
            }
            else
            {
                ab_full_path = mStreamingAssetsPath + ab_path;
            }


            //检查资源是否已加载
            if (mLoadedAssetBundlePool.ContainsKey(ab_path))
            {
                //确认资源来源位置
                if (mLoadedAssetBundlePool[ab_path].IsFromStreamingAssetsPath && loadFormPerPath)
                {
                    //当前要加载的文件优先级高于已加载的,要把已加载的释放掉
                    mLoadedAssetBundlePool[ab_path].ab_asset.Unload(false);
                    mLoadedAssetBundlePool.Remove(ab_path);
                }
                else
                {
                    //已经完美的加载出来了,不用管了
                    if (callback != null)
                    {
                        callback(mLoadedAssetBundlePool[ab_path].ab_asset);
                        yield break;
                    }
                }
            }

            //是否已在加载计划中
            if (mLoadPlane.ContainsKey(ab_full_path))
            {
                //已经有个协程在加载它了,把自己的任务登记进计划,然后不管了
                mLoadPlane[ab_full_path].AddCallback(callback);
                yield break;
            }
            else
            {
                //登记自己的加载计划
                mLoadPlane.Add(ab_full_path, new LoadPlane(ab_path, callback));
            }

#if UNITY_IOS || UNITY_STANDALONE
            //检查文件是否存在
            if (!File.Exists(ab_full_path))
            {
                XLog.PrintE("[TinaX][虚拟文件系统]加载资源错误,寻址路径:" + ab_path + "  ,实际检测路径:" + ab_full_path);
                //加载失败决定
                mLoadPlane[ab_full_path].Fire(null);
                mLoadPlane.Remove(ab_full_path);
                yield break;
            }
#endif



            //Debug.Log("    正式加载:" + ab_full_path);
            //如果在上面没有return 掉,就正式加载
            var ab_req = AssetBundle.LoadFromFileAsync(ab_full_path);
            yield return(ab_req);

            if (ab_req.assetBundle == null)
            {
                //加载失败
                mLoadPlane[ab_full_path].Fire(null);
                mLoadPlane.Remove(ab_full_path);
                yield break;
            }

            //登记
            var loaded_ab_data = new LoadedAssetBundle();
            loaded_ab_data.Init(ab_req.assetBundle, !loadFormPerPath, ab_path, ab_full_path, dependences);
            mLoadedAssetBundlePool.Add(ab_path, loaded_ab_data);

            //加载计划处理
            mLoadPlane[ab_full_path].Fire(ab_req.assetBundle);
            mLoadPlane.Remove(ab_full_path);
            yield break;
        }
Пример #2
0
        /// <summary>
        /// 递归加载AB包及其所有依赖
        /// </summary>
        /// <param name="ab_path_pure">ab包相对路径</param>
        /// <returns>返回路径对应的AB包</returns>
        private AssetBundle Load_AssetBundle_And_Dependences_Recursion(string ab_path)
        {
            //检查当前AB包的加载位置
            bool loadFormPerPath = IsLoadABFormPerPath(ab_path);

            string[] dependences; //依赖ab包
            if (loadFormPerPath && mABManifest_Per != null)
            {
                dependences = mABManifest_Per.GetAllDependencies(ab_path);
            }
            else
            {
                dependences = mABManifest_Stream.GetAllDependencies(ab_path);
            }


            //加载依赖包
            if (dependences.Length >= 0)
            {
                foreach (var item in dependences)
                {
                    if (Load_AssetBundle_And_Dependences_Recursion(item) != null)
                    {
                        Register_AB_Use(item);
                    }
                }
            }
            //加载AB本体
            //求当前ab的实际加载路径(绝对路径)
            string ab_full_path;

            if (loadFormPerPath)
            {
                ab_full_path = mPersistentDataPath + ab_path;
            }
            else
            {
                ab_full_path = mStreamingAssetsPath + ab_path;
            }


            //检查资源是否已加载
            if (mLoadedAssetBundlePool.ContainsKey(ab_path))
            {
                //确认资源来源位置
                if (mLoadedAssetBundlePool[ab_path].IsFromStreamingAssetsPath && loadFormPerPath)
                {
                    //当前要加载的文件优先级高于已加载的,要把已加载的释放掉
                    mLoadedAssetBundlePool[ab_path].ab_asset.Unload(false);
                    mLoadedAssetBundlePool.Remove(ab_path);
                }
                else
                {
                    //已经完美的加载出来了,不用管了
                    return(mLoadedAssetBundlePool[ab_path].ab_asset);
                }
            }

#if UNITY_IOS || UNITY_STANDALONE
            //检查文件是否存在
            if (!File.Exists(ab_full_path))
            {
                XLog.PrintE("[TinaX][虚拟文件系统]加载资源错误,寻址路径:" + ab_path + "  ,实际检测路径:" + ab_full_path);
                return(null);
            }
#endif


            //如果在上面没有return 掉,就正式加载
            var ab = AssetBundle.LoadFromFile(ab_full_path);
            //登记
            var loaded_ab_data = new LoadedAssetBundle();
            loaded_ab_data.Init(ab, !loadFormPerPath, ab_path, ab_full_path, dependences);
            mLoadedAssetBundlePool.Add(ab_path, loaded_ab_data);

            return(ab);
        }