Exemple #1
0
 protected override void OnCancelLoading()
 {
     if (webRequestOp != null)
     {
         webRequestOp.Dispose();
     }
 }
        protected override void OnCancelLoading()
        {
            if (loadCoroutine != null)
            {
                CoroutineStarter.Stop(loadCoroutine);
                loadCoroutine = null;
            }

            if (asyncOp != null)
            {
                asyncOp.Dispose();
            }

            for (int i = 0; i < dependencyPromises.Count; i++)
            {
                dependencyPromises[i].Unload();
            }

            dependencyPromises.Clear();

            if (asset != null)
            {
                asset.CancelShow();
            }

            UnregisterConcurrentRequest();
        }
Exemple #3
0
 private void OnDestroy()
 {
     if (loadOp != null)
     {
         loadOp.Dispose();
     }
 }
Exemple #4
0
        private WebRequestAsyncOperation SendWebRequest <T>(
            T requestType,
            string url,
            DownloadHandler downloadHandler,
            Action <UnityWebRequest> OnSuccess,
            Action <UnityWebRequest> OnFail,
            int requestAttemps,
            int timeout,
            bool disposeOnCompleted,
            Dictionary <string, string> headers = null
            ) where T : IWebRequest
        {
            int remainingAttemps = Mathf.Clamp(requestAttemps, 1, requestAttemps);

            UnityWebRequest request = requestType.CreateWebRequest(url);

            request.timeout = timeout;
            if (headers != null)
            {
                foreach (var item in headers)
                {
                    request.SetRequestHeader(item.Key, item.Value);
                }
            }

            if (downloadHandler != null)
            {
                request.downloadHandler = downloadHandler;
            }

            WebRequestAsyncOperation resultOp = new WebRequestAsyncOperation(request);

            resultOp.disposeOnCompleted = disposeOnCompleted;
            ongoingWebRequests.Add(resultOp);

            UnityWebRequestAsyncOperation requestOp = resultOp.webRequest.SendWebRequest();

            requestOp.completed += (asyncOp) =>
            {
                if (!resultOp.isDisposed)
                {
                    if (resultOp.webRequest.WebRequestSucceded())
                    {
                        OnSuccess?.Invoke(resultOp.webRequest);
                        resultOp.SetAsCompleted(true);
                    }
                    else if (!resultOp.webRequest.WebRequestAborted() && resultOp.webRequest.WebRequestServerError())
                    {
                        remainingAttemps--;
                        if (remainingAttemps > 0)
                        {
                            Debug.LogWarning($"Retrying web request: {url} ({remainingAttemps} attemps remaining)");
                            resultOp.Dispose();
                            resultOp = SendWebRequest(requestType, url, downloadHandler, OnSuccess, OnFail, remainingAttemps, timeout, disposeOnCompleted);
                        }
                        else
                        {
                            OnFail?.Invoke(resultOp.webRequest);
                            resultOp.SetAsCompleted(false);
                        }
                    }
                    else
                    {
                        OnFail?.Invoke(resultOp.webRequest);
                        resultOp.SetAsCompleted(false);
                    }
                }

                ongoingWebRequests.Remove(resultOp);
            };

            return(resultOp);
        }
        protected IEnumerator LoadAssetBundleWithDeps(string baseUrl, string hash, Action OnSuccess, Action OnFail)
        {
            string finalUrl = baseUrl + hash;

            if (failedRequestUrls.Contains(finalUrl))
            {
                OnFail?.Invoke();
                yield break;
            }

            yield return(WaitForConcurrentRequestsSlot());

            RegisterConcurrentRequest();
#if (UNITY_EDITOR || UNITY_STANDALONE)
            asyncOp = Environment.i.platform.webRequest.GetAssetBundle(url: finalUrl, hash: Hash128.Compute(hash), disposeOnCompleted: false);
#else
            //NOTE(Brian): Disable in build because using the asset bundle caching uses IDB.
            asyncOp = Environment.i.platform.webRequest.GetAssetBundle(url: finalUrl, disposeOnCompleted: false);
#endif

            if (!DependencyMapLoadHelper.dependenciesMap.ContainsKey(hash))
            {
                CoroutineStarter.Start(DependencyMapLoadHelper.GetDepMap(baseUrl, hash));
            }

            yield return(DependencyMapLoadHelper.WaitUntilDepMapIsResolved(hash));

            if (DependencyMapLoadHelper.dependenciesMap.ContainsKey(hash))
            {
                using (var it = DependencyMapLoadHelper.dependenciesMap[hash].GetEnumerator())
                {
                    while (it.MoveNext())
                    {
                        var dep     = it.Current;
                        var promise = new AssetPromise_AB(baseUrl, dep, containerTransform);
                        AssetPromiseKeeper_AB.i.Keep(promise);
                        dependencyPromises.Add(promise);
                    }
                }
            }

            yield return(asyncOp);

            if (asyncOp.isDisposed)
            {
                OnFail?.Invoke();
                yield break;
            }

            if (!asyncOp.isSucceded)
            {
                if (VERBOSE)
                {
                    Debug.Log($"Request failed? {asyncOp.webRequest.error} ... {finalUrl}");
                }
                failedRequestUrls.Add(finalUrl);
                OnFail?.Invoke();
                asyncOp.Dispose();
                yield break;
            }

            UnregisterConcurrentRequest();

            foreach (var promise in dependencyPromises)
            {
                yield return(promise);
            }

            AssetBundle assetBundle = DownloadHandlerAssetBundle.GetContent(asyncOp.webRequest);
            asyncOp.Dispose();

            if (assetBundle == null || asset == null)
            {
                OnFail?.Invoke();

                failedRequestUrls.Add(finalUrl);
                yield break;
            }

            asset.ownerAssetBundle     = assetBundle;
            asset.assetBundleAssetName = assetBundle.name;

            assetBundlesLoader.MarkAssetBundleForLoad(asset, assetBundle, containerTransform, OnSuccess, OnFail);
        }