public IEnumerator LoadAssetBundle(string sceneName, string bundleName, LoadCompleteDelegate handler = null, LoadAllCompleteDelegate allHandler = null)
        {
            if (string.IsNullOrWhiteSpace(sceneName))
            {
                throw new ArgumentException(FrameConst.PREFIX + "非法的字符串!", nameof(sceneName));
            }
            if (string.IsNullOrWhiteSpace(bundleName))
            {
                throw new ArgumentException(FrameConst.PREFIX + "非法的字符串!", nameof(bundleName));
            }

            //等待清单文件加载完成
            while (!ManifestMgr.Instance.IsLoadFinished)
            {
                yield return(null);
            }
            Manifest = ManifestMgr.Instance.GetManifest();

            //把当前场景加入集合中
            if (!multiBundleLoaderDict.ContainsKey(sceneName))
            {
                MultiBundleLoader multiBundleLoader = new MultiBundleLoader(handler, allHandler);
                multiBundleLoaderDict.Add(sceneName, multiBundleLoader);
            }
            //加载指定的AB包
            yield return(multiBundleLoaderDict[sceneName].LoadAssetBundle(bundleName));
        }
        /// <summary>构造函数</summary>
        /// <param name="bundleName">AssetBundle包名</param>
        /// <param name="handler">加载完成时调用的委托</param>
        internal SingleBundleLoader(string bundleName, LoadCompleteDelegate handler)
        {
//			if(string.IsNullOrEmpty(abName))
//				throw new ArgumentException(FrameConst.PREFIX + nameof(abName));

            this.bundleName     = bundleName;
            bundleDownloadPath  = PathTools.GetWWWPath() + "/" + this.bundleName;
            isLoadFinished      = false;
            loadCompleteHandler = handler;
        }
Example #3
0
		/// <summary>构造函数</summary>
		/// <param name="handler">加载完成时调用的委托</param>
		/// <param name="allHandler">全部加载完成时调用的委托</param>
		internal MultiBundleLoader(LoadCompleteDelegate handler,LoadAllCompleteDelegate allHandler) {
//			if(string.IsNullOrEmpty(sceneName))
//				throw new ArgumentException(FrameConst.PREFIX + nameof(sceneName));
//			if(string.IsNullOrEmpty(bundleName))
//				throw new ArgumentException(FrameConst.PREFIX + nameof(bundleName));

			singleBundleLoaderDict = new Dictionary<string, SingleBundleLoader>();
			bundleRelationDict = new Dictionary<string, BundleRelation>();
			loadCompleteHandler = handler;
			loadAllCompleteHandler = allHandler;
		}
Example #4
0
        /// <summary>Starts the loading, setting the current image to <see cref="_LoadingTexture"/>, if available. If the image is already in cache, and <paramref name="loadCachedIfAvailable"/>==true, will load that instead</summary>
        public void Load(string imageURL, bool loadCachedIfAvailable = true, LoadCompleteDelegate onCompleted = null, Action onCanceled = null)
        {
            bool currentRequestedURLAlreadyLoaded = _CurrentRequestedURL == imageURL;

            _CurrentRequestedURL = imageURL;

            if (loadCachedIfAvailable)
            {
                bool foundCached = false;
                // Don't re-request if the url is the same. This is useful if there's no pool provided
                if (currentRequestedURLAlreadyLoaded)
                {
                    foundCached = _Texture != null;
                }
                else if (_Pool != null)
                {
                    Texture2D cachedInPool = _Pool.Get(imageURL) as Texture2D;
                    if (cachedInPool)
                    {
                        _Texture             = cachedInPool;
                        foundCached          = true;
                        _CurrentRequestedURL = imageURL;
                    }
                }

                if (foundCached)
                {
                    _RawImage.texture = _Texture;
                    if (onCompleted != null)
                    {
                        onCompleted(true, true);
                    }

                    return;
                }
            }

            _RawImage.texture = _LoadingTexture;
            var request = new SimpleImageDownloader.Request()
            {
                url    = imageURL,
                onDone = result =>
                {
                    if (!_DestroyPending && imageURL == _CurrentRequestedURL) // this will be false if a new request was done during downloading, case in which the result will be ignored
                    {
                        // Commented: not reusing textures to load data into them anymore, since in most cases we'll use a pool
                        //result.LoadTextureInto(_Texture);

                        if (_Pool == null)
                        {
                            // Non-pooled textures should be destroyed
                            if (_Texture)
                            {
                                DisposeTexture(_Texture);
                            }

                            _Texture = result.CreateTextureFromReceivedData();
                        }
                        else
                        {
                            var  textureAlreadyStoredMeanwhile = _Pool.Get(imageURL);
                            bool someoneStoredTheImageSooner   = textureAlreadyStoredMeanwhile != null;
                            if (someoneStoredTheImageSooner)
                            {
                                // Happens when the same URL is requested multiple times for the first time, and of course only the first
                                // downloaded image should be kept. In this case, someone else already have downloaded and cached the image, so we just discard the one we downloaded
                                _Texture = textureAlreadyStoredMeanwhile as Texture2D;
                            }
                            else
                            {
                                // First time downloaded => cache
                                _Texture = result.CreateTextureFromReceivedData();
                                _Pool.Put(imageURL, _Texture);
                            }
                        }

                        _RawImage.texture = _Texture;

                        if (onCompleted != null)
                        {
                            onCompleted(false, true);
                        }
                    }
                    else if (onCanceled != null)
                    {
                        onCanceled();
                    }
                },
                onError = () =>
                {
                    if (!_DestroyPending && imageURL == _CurrentRequestedURL)                     // this will be false if a new request was done during downloading, case in which the result will be ignored
                    {
                        _RawImage.texture = _ErrorTexture;

                        if (onCompleted != null)
                        {
                            onCompleted(false, false);
                        }
                    }
                    else if (onCanceled != null)
                    {
                        onCanceled();
                    }
                }
            };

            SimpleImageDownloader.Instance.Enqueue(request);
        }