Ejemplo n.º 1
0
        private WebRequestAsyncOperation SendWebRequest <T>(
            T requestType,
            string url,
            Action <UnityWebRequest> OnSuccess,
            Action <string> OnFail,
            int requestAttemps,
            int timeout) where T : IWebRequest
        {
            int remainingAttemps = Mathf.Clamp(requestAttemps, 1, requestAttemps);

            UnityWebRequest request = requestType.CreateWebRequest(url);

            request.timeout = timeout;

            WebRequestAsyncOperation resultOp = new WebRequestAsyncOperation(request);

            ongoingWebRequests.Add(resultOp);

            UnityWebRequestAsyncOperation requestOp = resultOp.webRequest.SendWebRequest();

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

                ongoingWebRequests.Remove(resultOp);
            };

            return(resultOp);
        }
Ejemplo n.º 2
0
        public void UpdateCulling()
        {
            if (owner == null)
            {
                return;
            }

            if (rt == null)
            {
                rt = transform as RectTransform;
            }

            Vector2 myMinCoords = rt.TransformPoint(new Vector3(rt.rect.xMin, rt.rect.yMin));
            Vector2 myMaxCoords = rt.TransformPoint(new Vector3(rt.rect.xMax, rt.rect.yMax));

            Vector2 viewMinCoords = owner.viewport.TransformPoint(new Vector3(owner.viewport.rect.xMin, owner.viewport.rect.yMin));
            Vector2 viewMaxCoords = owner.viewport.TransformPoint(new Vector3(owner.viewport.rect.xMax, owner.viewport.rect.yMax));

#if UNITY_EDITOR
            if (VERBOSE)
            {
                var rtWorldRect = new Rect(myMinCoords.x, myMinCoords.y, myMaxCoords.x - myMinCoords.x, myMaxCoords.y - myMinCoords.y);
                Utils.DrawRectGizmo(rtWorldRect, Color.red, 5f);
            }
#endif
            float size = (viewMaxCoords - viewMinCoords).magnitude;

            Rect viewportRect = new Rect(viewMinCoords, viewMaxCoords - viewMinCoords);
            viewportRect.min -= Vector2.one * size;
            viewportRect.max += Vector2.one * size;

#if UNITY_EDITOR
            if (VERBOSE)
            {
                Utils.DrawRectGizmo(viewportRect, Color.blue, 5f);
            }
#endif

            Rect myRect  = new Rect(myMinCoords, myMaxCoords - myMinCoords);
            bool visible = viewportRect.Overlaps(myRect, true);

            targetImage.enabled = visible;

            if (!isLoadingOrLoaded)
            {
                loadOp = LoadChunkImage();
            }
        }
Ejemplo n.º 3
0
        protected override void OnLoad(Action OnSuccess, Action OnFail)
        {
            // Reuse the already-stored default texture, we duplicate it and set the needed config afterwards in AddToLibrary()
            if (library.Contains(idWithDefaultTexSettings) && !UsesDefaultWrapAndFilterMode())
            {
                OnSuccess?.Invoke();
                return;
            }

            if (!url.StartsWith(PLAIN_BASE64_PROTOCOL))
            {
                webRequestOp = DCL.Environment.i.platform.webRequest.GetTexture(
                    url: url,
                    OnSuccess: (webRequestResult) =>
                {
                    if (asset != null)
                    {
                        asset.texture = DownloadHandlerTexture.GetContent(webRequestResult);
                        if (IsQuestionMarkPNG(asset.texture))
                        {
                            OnFail?.Invoke();
                        }
                        else
                        {
                            OnSuccess?.Invoke();
                        }
                    }
                    else
                    {
                        OnFail?.Invoke();
                    }
                },
                    OnFail: (webRequestResult) =>
                {
                    OnFail?.Invoke();
                });
            }
            else
            {
                //For Base64 protocols we just take the bytes and create the texture
                //to avoid Unity's web request issue with large URLs
                byte[] decodedTexture = Convert.FromBase64String(url.Substring(PLAIN_BASE64_PROTOCOL.Length));
                asset.texture = new Texture2D(1, 1);
                asset.texture.LoadImage(decodedTexture);
                OnSuccess?.Invoke();
            }
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 5
0
        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);
        }