示例#1
0
 public void DownloadBundle(string url, OnAssetBundleLoaded onAssetBundleLoaded, OnDownloadProgress onDownloadProgress)
 {
     //if (coWebReq != null)
     //    StopCoroutine(coWebReq);
     //coWebReq =
     StartCoroutine(RequestBundle(url, 3600f, onAssetBundleLoaded, onDownloadProgress));
 }
    // LoadBUndle is triggered from the LoadBundle button
    private IEnumerator Loadbundle()
    {
        var bundleLoadRequest = AssetBundle.LoadFromFileAsync(s3example.AssetBundlePath);

        yield return(bundleLoadRequest);

        loadedAssetBundle = bundleLoadRequest.assetBundle;
        // If there are listeners to the event, trigger the event
        OnAssetBundleLoaded?.Invoke();

        Debug.Log("Assetbundle loaded");
    }
示例#3
0
    IEnumerator RequestBundle(string url, float timeOut, OnAssetBundleLoaded onAssetBundleLoaded, OnDownloadProgress onDownloadProgress)
    {
        UnityWebRequest webRequest = UnityWebRequest.Get(url);

        webRequest.chunkedTransfer = false;
        webRequest.SetRequestHeader("Accept", "*/*");
        webRequest.SetRequestHeader("Accept-Language", "en-US");
        webRequest.SetRequestHeader("Content-Language", "en-US");
        webRequest.SetRequestHeader("Accept-Encoding", "gzip, deflate");
        webRequest.SetRequestHeader("User-Agent", "runscope/0.1");
        UnityWebRequestAsyncOperation handler = webRequest.SendWebRequest();


        float timeIn    = 0f;
        bool  isAborted = false;

        while (!handler.isDone)
        {
            timeIn += Time.deltaTime;
            if (onDownloadProgress != null)
            {
                onDownloadProgress(handler.progress);
            }
            if (timeIn > timeOut)
            {
                //Security
                isAborted = true;
                webRequest.Abort();
                break;
            }
            yield return(null);
        }

        if (webRequest.isNetworkError || webRequest.isHttpError || isAborted)
        {
            Debug.Log(webRequest.error);
            onAssetBundleLoaded(null);
        }
        else
        {
            AssetBundleCreateRequest bundleRequest = AssetBundle.LoadFromMemoryAsync(webRequest.downloadHandler.data);

            timeIn    = 0f;
            isAborted = false;

            while (!bundleRequest.isDone)
            {
                timeIn += Time.deltaTime;
                if (onDownloadProgress != null)
                {
                    onDownloadProgress(bundleRequest.progress);
                }
                if (timeIn > timeOut)
                {
                    //Security
                    isAborted = true;
                    webRequest.Abort();
                    break;
                }
                yield return(null);
            }

            AssetBundle bundle = bundleRequest.assetBundle;
            if (bundle == null)
            {
                Debug.Log("Bundle error");
            }
            onAssetBundleLoaded(bundle);
        }

        yield break;
    }