Пример #1
0
        /// <summary>
        /// 返回链接的底层加载器的状态
        /// </summary>
        /// <param name="url"></param>
        /// <param name="birdgeLoader"></param>
        /// <returns></returns>
        private IEnumerator LoadAssetAsync(string url, BridgeLoader bridgeLoader)
        {
            bridgeLoader.m_ResourcesUrl = url;
            if (bridgeLoader.m_ConnectLoader == null)
            {
                OnLoadAssetFail();
                yield break;
            }

            while (bridgeLoader.m_ConnectLoader.IsCompleted == false)
            {
                yield return(null);
            }

            if (bridgeLoader.m_ConnectLoader.GetType() == typeof(NewAssetBundleLoader3))
            {
                string fileName = System.IO.Path.GetFileNameWithoutExtension(url);
                NewAssetBundleLoader3 assetBundleloader = bridgeLoader.m_ConnectLoader as NewAssetBundleLoader3;
                object asset = assetBundleloader.AssetLoaderResultInfor.GetAssetByUrl(assetBundleloader.m_ResourcesUrl, fileName);
                OnCompleteLoad(bridgeLoader.m_ConnectLoader.IsError, bridgeLoader.m_ConnectLoader.Description, asset, bridgeLoader.m_ConnectLoader.IsCompleted);
            }
            else
            {
                OnCompleteLoad(bridgeLoader.m_ConnectLoader.IsError, bridgeLoader.m_ConnectLoader.Description, bridgeLoader.m_ConnectLoader.ResultObj, bridgeLoader.m_ConnectLoader.IsCompleted);
            }
        }
Пример #2
0
        /// <summary>
        /// 同步加载资源 (这里处理的是最外层的AssetBundle ,当前资源依赖的AssetBundle 处理在LoadDepdenceAssetBundleAsync)
        /// </summary>
        /// <param name="url"></param>
        protected void LoadAssetBundleSync(string url, string assetFileName, AssetBundleExitState assetBundleState, bool isloadScene)
        {
            m_ResourcesUrl = url;
            if (System.IO.Path.GetExtension(m_ResourcesUrl) != ConstDefine.AssetBundleExtensionName)
            {
                m_ResourcesUrl += ConstDefine.AssetBundleExtensionName;  //组合上扩展名
            }
            #region  加载依赖的资源
            string[] dependenceAssets = AssetBundleMgr.Instance.S_AssetBundleManifest.GetAllDependencies(m_ResourcesUrl); //获取所有的依赖文件
            List <NewAssetBundleLoader3> allDependenceAssetLoader = new List <NewAssetBundleLoader3>();                   //所有依赖文件的加载器
            foreach (var item in dependenceAssets)
            {
                bool isLoaderExit = false;
                NewAssetBundleLoader3 subAssetBundleLoader = ResourcesLoaderMgr.GetOrCreateLoaderInstance <NewAssetBundleLoader3>(item, ref isLoaderExit);
                if (isLoaderExit && subAssetBundleLoader.IsCompleted)
                {
                    Debug.LogEditorInfor("LoadAssetBundleASync Exit ," + item);
                    continue;  //已经存在了 则继续
                }
                allDependenceAssetLoader.Add(subAssetBundleLoader);
                LoadDepdenceAssetBundleSync(item, subAssetBundleLoader, assetBundleState);
            } //递归加载资源的依赖项


            for (int dex = 0; dex < allDependenceAssetLoader.Count; ++dex)
            {
                if (allDependenceAssetLoader[dex].IsError)
                {
                    Debug.LogError("[AssetBundleLoader] LoadAssetBundleSync  Fail, 依赖的 AssetBundle 资源不存在 " + allDependenceAssetLoader[dex].m_ResourcesUrl);
                    OnCompleteLoad(false, string.Format("[AssetBundleLoader] LoadAssetBundle Fail,AssetBundle Path= {0}", allDependenceAssetLoader[dex].m_ResourcesUrl), null, true);
                    break;
                }
            } //判断依赖项是否加载完成
            #endregion


            #region   AssetBundle  同步加载
            AssetLoaderResultInfor.LoadedAssetBundle = AssetBundle.LoadFromFile(S_AssetBundleTopPath + m_ResourcesUrl);
            if (AssetLoaderResultInfor.LoadedAssetBundle == null)
            {
                ResultObj = null;
                OnCompleteLoad(true, string.Format("同步加载本地AssetBundle 失败{0}", url), null, true, 1);
                return;
            }
            LoadAndRecordAsset(AssetLoaderResultInfor, assetBundleState, isloadScene);

            OnCompleteLoad(AssetLoaderResultInfor.LoadedAssetBundle == null, string.Format("[AssetBundleLoader] LoadAssetBundleSuccess  {0}", m_ResourcesUrl), null, true);
            #endregion

            Debug.LogInfor("加载AssetBundle  成功");
        }
Пример #3
0
        /// <summary>
        /// AssetBundle 依赖的AssetBundle 完成加载
        /// (注意必须给出一个指向当前子AssetBundleLoader 的参数 ,否则操作的是外层AssetBundleLoader 的参数而导致状态异常)
        /// </summary>
        /// <param name="AssetLoader"></param>
        /// <param name="isError"></param>
        /// <param name="description"></param>
        /// <param name="result"></param>
        /// <param name="iscomplete"></param>
        /// <param name="process"></param>
        protected void OnCompleteLoadDepdenceAsset(NewAssetBundleLoader3 AssetLoader, bool isError, string description, object result, bool iscomplete, float process = 1)
        {
            if (AssetLoader == null)
            {
                return;
            }

            AssetLoader.IsCompleted = iscomplete;
            AssetLoader.IsError     = isError;
            AssetLoader.Description = description;
            AssetLoader.ResultObj   = result;
            AssetLoader.Process     = process;

            AssetLoader.AssetLoaderResultInfor.m_AllNeedLoadAssetRecord.Clear();
        }
Пример #4
0
        /// <summary>
        /// (同步加载)加载AssetBundle 资源
        /// </summary>
        /// <param name="url">相对于AseetBundle 资源存放路径的路径 (如果是打包成整个预制体则是整个预制体的路径)</param>
        /// <param name="assetFileName">实际加载AssetBundle 时候的文件名称(考虑到加载整个AssetBundle 中一个资源的情况)</param>
        /// <param name="onCompleteAct">加载完成回调</param>
        ///  <param name="isloadSceneAsset"> 如果加载的是场景 则这里必须填true ,否则false</param>
        /// <returns></returns>
        private static NewAssetBundleLoader3 LoadAssetBundleAssetSync(string url, string assetFileName, AssetBundleExitState assetBundleState, bool isloadSceneAsset)
        {
            if (string.IsNullOrEmpty(url))
            {
                Debug.LogError(string.Format("Url Can't Be Null , TypeLoader={0}", typeof(NewAssetBundleLoader3)));
                return(null);
            }

            if (assetBundleState == AssetBundleExitState.None)
            {
                Debug.LogError("LoadAssetBundleAssetSync Fail,Not AssetBundle Asset" + url + "    assetFileName=" + assetFileName);
                return(null);
            }

            Debug.LogInfor("LoadAssetBundleAsset  url=" + url + "    assetFileName=" + assetFileName);
            bool isLoaderExit = false;
            NewAssetBundleLoader3 assetBundleLoader = ResourcesLoaderMgr.GetOrCreateLoaderInstance <NewAssetBundleLoader3>(url, ref isLoaderExit);

            if (isLoaderExit && assetBundleLoader.IsCompleted)
            {
                assetBundleLoader.LoadassetModel = LoadAssetModel.Sync;

                //*** 判断当前资源是否加载了
                if (string.IsNullOrEmpty(assetFileName) == false && assetBundleLoader.AssetLoaderResultInfor.m_AllNeedLoadAssetRecord.Contains(assetFileName) == false)
                {
                    assetBundleLoader.AssetLoaderResultInfor.m_AllNeedLoadAssetRecord.Add(assetFileName);
                }
                assetBundleLoader.LoadAndRecordAsset(assetBundleLoader.AssetLoaderResultInfor, assetBundleState, isloadSceneAsset); //加载已经加载的AssetBundle 中的资源

                assetBundleLoader.OnCompleteLoad(assetBundleLoader.IsError, assetBundleLoader.Description, null, true);             //如果当前加载器已经完成加载 则手动触发事件
                return(assetBundleLoader);                                                                                          //如果已经存在 且当前加载器还在加载中,则只需要等待加载完成则回调用回调
            }
            if (assetBundleLoader.LoadassetModel == LoadAssetModel.Async)
            {
                assetBundleLoader.ForceBreakLoaderProcess();
            } //结束之前的加载进程

            assetBundleLoader.LoadassetModel = LoadAssetModel.Sync;
            assetBundleLoader.AssetLoaderResultInfor.SetAssetName(url, assetBundleState);
            assetBundleLoader.m_LoadAssetCoroutine = null;
            if (string.IsNullOrEmpty(assetFileName) == false)
            {
                assetBundleLoader.AssetLoaderResultInfor.m_AllNeedLoadAssetRecord.Add(assetFileName);
            }
            assetBundleLoader.LoadAssetBundleSync(url, assetFileName, assetBundleState, isloadSceneAsset);
            return(assetBundleLoader);
        }
Пример #5
0
        /// <summary>
        /// 加载AssetBundle 资源
        /// </summary>
        /// <param name="url">相对于AseetBundle 资源存放路径的路径 (如果是打包成整个预制体则是整个预制体的路径)</param>
        /// <param name="assetFileName">实际加载AssetBundle 时候的文件名称(考虑到加载整个AssetBundle 中一个资源的情况)</param>
        /// <param name="onCompleteAct">加载完成回调</param>
        ///  <param name="isloadSceneAsset"> 如果加载的是场景 则这里必须填true ,否则false</param>
        /// <returns></returns>
        private static NewAssetBundleLoader3 LoadAssetBundleAssetAsync(string url, string assetFileName, AssetBundleExitState assetBundleState, bool isloadSceneAsset)
        {
            if (string.IsNullOrEmpty(url) || string.IsNullOrEmpty(assetFileName))
            {
                Debug.LogError(string.Format("Url Can't Be Null , TypeLoader={0}", typeof(NewAssetBundleLoader3)));
                return(null);
            }

            if (assetBundleState == AssetBundleExitState.None)
            {
                Debug.LogError("LoadAssetBundleAssetSync Fail,Not AssetBundle Asset" + url + "    assetFileName=" + assetFileName);
                return(null);
            }

            Debug.LogInfor("LoadAssetBundleAsset  url=" + url + "    assetFileName=" + assetFileName);
            bool isLoaderExit = false;
            NewAssetBundleLoader3 assetBundleLoader = ResourcesLoaderMgr.GetOrCreateLoaderInstance <NewAssetBundleLoader3>(url, ref isLoaderExit);

            assetBundleLoader.LoadassetModel = LoadAssetModel.Async; //这里貌似没必要(由于异步加载时候同步加载必定完成了)

            if (isLoaderExit)
            {
                if (assetBundleLoader.IsCompleted)
                {
                    assetBundleLoader.LoadAndRecordAsset(assetBundleLoader.AssetLoaderResultInfor, assetBundleState, isloadSceneAsset);
                    assetBundleLoader.OnCompleteLoad(assetBundleLoader.IsError, assetBundleLoader.Description, null, true);  //如果当前加载器已经完成加载 则手动触发事件
                }
                else
                {
                    assetBundleLoader.AssetLoaderResultInfor.m_AllNeedLoadAssetRecord.Add(assetFileName); //记录需要加载这个指定的资源
                }
                return(assetBundleLoader);                                                                //如果已经存在 且当前加载器还在加载中,则只需要等待加载完成则回调用回调
            }
            assetBundleLoader.AssetLoaderResultInfor.SetAssetName(url, assetBundleState);
            assetBundleLoader.AssetLoaderResultInfor.m_AllNeedLoadAssetRecord.Add(assetFileName);  //记录需要加载这个指定的资源
            assetBundleLoader.m_LoadAssetCoroutine = EventCenter.Instance.StartCoroutine(assetBundleLoader.LoadAssetBundleASync(url, assetFileName, assetBundleState, isloadSceneAsset));
            return(assetBundleLoader);
        }
Пример #6
0
        /// <summary>
        /// 加载当前AssetBundle 依赖的资源(注意 : 这里加载成功之后不能直接生成)
        /// </summary>
        /// <param name="url"></param>
        /// <param name="assetBundleLoade"></param>
        /// <returns></returns>
        protected virtual IEnumerator LoadDepdenceAssetBundleAsync(string url, NewAssetBundleLoader3 depdebceAssetBundleLoader)
        {
            Debug.Log("LoadDepdenceAssetBundleAsync  url=" + url);
            depdebceAssetBundleLoader.m_ResourcesUrl = url;
            if (System.IO.Path.GetExtension(depdebceAssetBundleLoader.m_ResourcesUrl) != ConstDefine.AssetBundleExtensionName)
            {
                depdebceAssetBundleLoader.m_ResourcesUrl += ConstDefine.AssetBundleExtensionName;                                                   //组合上扩展名
            }
            string[] dependenceAssets = AssetBundleMgr.Instance.S_AssetBundleManifest.GetAllDependencies(depdebceAssetBundleLoader.m_ResourcesUrl); //获取所有的依赖文件
            List <NewAssetBundleLoader3> allDependenceAssetLoader = new List <NewAssetBundleLoader3>();                                             //所有依赖文件的加载器

            foreach (var item in dependenceAssets)
            {
                bool isLoaderExit = false;
                NewAssetBundleLoader3 subAssetBundleLoader = ResourcesLoaderMgr.GetOrCreateLoaderInstance <NewAssetBundleLoader3>(item, ref isLoaderExit);

                if (isLoaderExit && subAssetBundleLoader.IsCompleted)
                {
                    Debug.LogEditorInfor("LoadDepdenceAssetBundleAsync Exit ," + item);
                    continue;
                }
                allDependenceAssetLoader.Add(subAssetBundleLoader);
                Debug.Log("LoadDepdenceAssetBundleAsync  allDependenceAssetLoader   item=" + item);
                EventCenter.Instance.StartCoroutine(LoadDepdenceAssetBundleAsync(item, subAssetBundleLoader));
            } //递归加载资源的依赖项


            for (int dex = 0; dex < allDependenceAssetLoader.Count; ++dex)
            {
                while (allDependenceAssetLoader[dex].IsCompleted == false)
                {
                    yield return(null);
                }

                if (allDependenceAssetLoader[dex].IsError)
                {
                    Debug.LogError("[AssetBundleLoader] LoadDepdenceAssetBundleAsync  Fail, 依赖的 AssetBundle " + url + " 资源不存在 " + allDependenceAssetLoader[dex].m_ResourcesUrl);
                    OnCompleteLoadDepdenceAsset(allDependenceAssetLoader[dex], false,
                                                string.Format("[AssetBundleLoader] LoadAssetBundle Fail,AssetBundle Path={0}" + allDependenceAssetLoader[dex].m_ResourcesUrl), null, true);
                    yield break;
                }
            } //判断依赖项是否加载完成


            #region  AssetBundleCreateRequest 加载
            string fileAbsolutelyPath = string.Format("{0}{1}", S_AssetBundleTopPath, url);  //当先需要下载的资源的绝对路径

            AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(fileAbsolutelyPath);
            yield return(request);

            if (request.isDone)
            {
                Debug.Log("LoadDepdenceAssetBundleAsync Success  depdebceAssetBundleLoader.m_ResourcesUrl=" + depdebceAssetBundleLoader.m_ResourcesUrl);
                depdebceAssetBundleLoader.AssetLoaderResultInfor.LoadedAssetBundle = request.assetBundle;
            }
            else
            {
                depdebceAssetBundleLoader.AssetLoaderResultInfor.LoadedAssetBundle = null;
                Debug.LogError("LoadDepdenceAssetBundleAsync  FailAA==  " + url);
                Debug.LogError("LoadDepdenceAssetBundleAsync  Failbb==  " + fileAbsolutelyPath);
            }
            depdebceAssetBundleLoader.OnCompleteLoad(depdebceAssetBundleLoader.AssetLoaderResultInfor.LoadedAssetBundle == null, string.Format("[AssetBundleLoader] Load DepdenceAssetBundel Success {0}", depdebceAssetBundleLoader.m_ResourcesUrl), null, true);

            #endregion

            //Debug.LogInfor("加载AssetBundle  成功");
            yield break;
        }
Пример #7
0
        /// <summary>
        /// 根据配置的路径加载优先级 选择合适的加载器加载资源  (可能存在加载不到资源的情况,目前只处理LoadAssetPathEnum.PersistentDataPath和PersistentDataPath.ResourcesPath)
        /// </summary>
        /// <param name="bridgeLoader"></param>
        /// <param name="url"></param>
        /// <param name="isloadScene"> 如果加载的是场景 则这里必须填true ,否则false</param>
        /// <returns></returns>
        private IEnumerator LoadAssetByPriorityAsync(string url, BridgeLoader bridgeLoader, bool isloadSceneAsset)
        {
            LoadAssetPathEnum curLoadAssetPathEnum = ApplicationConfig.Instance.GetFirstPriortyAssetPathEnum();  //加载的优先级
            string            fileName             = System.IO.Path.GetFileNameWithoutExtension(url);

            do
            {
                if (curLoadAssetPathEnum == LoadAssetPathEnum.PersistentDataPath)
                {
                    AssetBundleExitState assetBundleExitState;
                    string newUrl = HotUpdate.AssetBundleMgr.Instance.CheckIfAssetBundleAsset(url.ToLower(), out assetBundleExitState);

                    if (assetBundleExitState != AssetBundleExitState.None)
                    {
                        Debug.Log("[AssetBundler ]加载外部资源,且以AssetBundle 加载");
                        bridgeLoader.m_ConnectLoader = NewAssetBundleLoader3.LoadAssetBundleAsset(newUrl, fileName, LoadAssetModel.Async, assetBundleExitState, isloadSceneAsset);   //整体打包的资源
                    }
                    else
                    {
                        if (isloadSceneAsset == false)
                        {
                            Debug.Log("[byteLoader ]优先加载外部资源,但是不是AssetBundle 资源,则以Byte[] 尝试 加载");
                            string path = url;
                            if (url.StartsWith(ConstDefine.S_AssetBundleTopPath) == false)
                            {
                                path = ConstDefine.S_AssetBundleTopPath + url;
                            }
                            bridgeLoader.m_ConnectLoader = ByteLoader.LoadAsset(path, LoadAssetModel.Async, null);
                        }
                        else
                        {
                            //***场景资源不通过这种方式
                        }
                    }
                }
                else if (curLoadAssetPathEnum == LoadAssetPathEnum.ResourcesPath)
                {
                    Debug.Log("[RecourcsLoader ]  加载Resources 资源 " + url);
                    bridgeLoader.m_ConnectLoader = ResourcesLoader.LoadResourcesAsset(url, LoadAssetModel.Async, null, isloadSceneAsset);
                }

                while (bridgeLoader.m_ConnectLoader.IsCompleted == false)
                {
                    yield return(null);
                }

                if (curLoadAssetPathEnum == LoadAssetPathEnum.PersistentDataPath && (bridgeLoader.m_ConnectLoader.GetType() == typeof(NewAssetBundleLoader3)))
                {
                    NewAssetBundleLoader3 assetBundleLoader = bridgeLoader.m_ConnectLoader as NewAssetBundleLoader3;
                    if (assetBundleLoader.AssetLoaderResultInfor.GetAssetByUrl(assetBundleLoader.m_ResourcesUrl, fileName) != null)
                    {
                        yield break;
                    }
                }
                else
                {
                    if (bridgeLoader.m_ConnectLoader.ResultObj != null)
                    {
                        yield break;
                    }
                }
                //    bridgeLoader.m_ConnectLoader.ReduceReference(bridgeLoader.m_ConnectLoader, false);  //卸载这个加载器
                ApplicationConfig.Instance.GetNextLoadAssetPath(ref curLoadAssetPathEnum);
                continue;  //如果加载得到则返回否则继续尝试其他的加载方式
            } while (curLoadAssetPathEnum != LoadAssetPathEnum.None);
            Debug.LogError("如果加载成功不会执行到这里" + url);
            bridgeLoader.m_ConnectLoader = null;  //如果加载成功不会执行到这里
        }