public override bool Update()
        {
            if (operation != null)
            {
                return(false);
            }

            AssetBundleRes bundle = AssetsBundleManager.GetLoadedAssetBundle(assetBundleName, out downloadingError);

            if (bundle != null)
            {
                if (isAdditive)
                {
                    operation = SceneManager.LoadSceneAsync(sceneName);
                }
                else
                {
                    operation = SceneManager.LoadSceneAsync(sceneName);
                }
                return(false);
            }
            else
            {
                return(true);
            }
        }
        /// <summary>
        /// 如果尚未下载,则设置给定资产包的下载操作。
        /// </summary>
        protected static bool LoadAssetBundleInternal(string _assetbundleName, bool _isLoadingManifest)
        {
            AssetBundleRes asset = null;

            mLoadedAssetBundles.TryGetValue(_assetbundleName, out asset);
            if (asset != null)
            {
                asset.mReferencedCount++;
                return(true);
            }
            //我们是否需要考虑引用的WWW数量?
            //在演示中,我们永远不会有重复的WWW,因为我们在调用另一个LoadAssetAsync()/ LoadLevelAsync()之前等待LoadAssetAsync()/ LoadLevelAsync()完成。
            //但在实际情况中,用户可以多次调用LoadAssetAsync()/ LoadLevelAsync(),然后等待它们完成,这可能有重复的WWW。
            if (mDownloadingBundles.Contains(_assetbundleName))
            {
                return(true);
            }
            string assetBaseDownloadingURL = GetAssetBundleBaseDownloadingURL(_assetbundleName);

            if (assetBaseDownloadingURL.ToLower().StartsWith("odr://"))
            {
                new ApplicationException("Can't load bundle " + _assetbundleName + " through ODR: this Unity version or build target doesn't support it.");
            }
            else if (assetBaseDownloadingURL.ToLower().StartsWith("res://"))
            {
                new ApplicationException("Can't load bundle " + _assetbundleName + " through asset catalog: this Unity version or build target doesn't support it.");
            }
            else
            {
                UnityWebRequest rq = null;
                //WWW download = null;
                if (!assetBaseDownloadingURL.EndsWith("/"))
                {
                    assetBaseDownloadingURL += "/";
                }
                string url = assetBaseDownloadingURL + _assetbundleName;
                //对于manifest assetbundle,总是下载它,因为我们没有哈希值。
                rq = UnityWebRequest.Get(url);
                //if (_isLoadingManifest)

                //else
                //    rq = // WWW.LoadFromCacheOrDownload(url, mAssetBundleManifest.GetAssetBundleHash(_assetbundleName), 0);

                // mProgressOperations.Add(new AssetBundleFromWeb(_assetbundleName,download));
            }
            mDownloadingBundles.Add(_assetbundleName);
            return(false);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 如果需要更多Update调用,则返回true。
        /// </summary>
        /// <returns></returns>
        public override bool Update()
        {
            if (request != null)
            {
                return(false);
            }
            AssetBundleRes assetBundleRes = AssetsBundleManager.GetLoadedAssetBundle(assetBundleName, out downloadingError);

            if (assetBundleRes != null)
            {
                ///@TODO: 当资产包下载失败时,会抛出异常。
                request = assetBundleRes.assetbundle.LoadAssetAsync(assetName, this.type);
                return(false);
            }
            else
            {
                return(true);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 下载完成后调用,实例化AssetBundleRes
        /// </summary>
        protected override void FinishDownload()
        {
            error = mWww.error;
            if (!string.IsNullOrEmpty(error))
            {
                return;
            }
            AssetBundle _assetBundle = mWww.assetBundle;

            if (_assetBundle == null)
            {
                error = string.Format("{0} 无效assetbundle资源", AssetbundleName);
            }
            else
            {
                assetBundleRes = new AssetBundleRes(mWww.assetBundle);
            }
            mWww.Dispose();
            mWww = null;
        }
        /// <summary>
        /// 检索先前通过LoadAssetBundle请求的资产包。
        /// 如果尚未下载资产包或其中一个依赖项,则返回null。
        /// </summary>
        /// <param name="_assetbundleName"></param>
        /// <param name="error"></param>
        /// <returns></returns>
        public static AssetBundleRes GetLoadedAssetBundle(string _assetbundleName, out string error)
        {
            if (mDownloadingErrors.TryGetValue(_assetbundleName, out error))
            {
                return(null);
            }

            AssetBundleRes loadedAB = null;

            mLoadedAssetBundles.TryGetValue(_assetbundleName, out loadedAB);
            if (loadedAB == null)
            {
                return(null);
            }

            string[] dependcies = null;
            if (!mDependencies.TryGetValue(_assetbundleName, out dependcies))
            {
                return(loadedAB);
            }

            //确保加载所有依赖项
            foreach (string dependcie in dependcies)
            {
                if (mDownloadingErrors.TryGetValue(dependcie, out error))
                {
                    return(null);
                }
                //等待所有依赖的assetsBundle被加载。
                AssetBundleRes dependentBundle;
                mLoadedAssetBundles.TryGetValue(dependcie, out dependentBundle);
                if (dependentBundle == null)
                {
                    return(null);
                }
            }
            return(loadedAB);
        }