Show Progress of UnityWebRequest

2019-08-18 07:08发布

问题:

I'm trying to download an assetbundle using Unity Web Request and show the progress, according to the documentation i need to capture a WebRequestAsyncOperation object to find the progress but i cannot find it

I tried using AsyncOperation and UnityWebRequestAsyncOperation and my routine works with both, what is the difference of using one or another?

here is my code:

IEnumerator DownloadModel3D()
    {
        using (UnityWebRequest uwr = UnityWebRequest.GetAssetBundle(bundleURL,1,0))
        {
            //UnityWebRequestAsyncOperation request = uwr.SendWebRequest();
            AsyncOperation request = uwr.SendWebRequest();

            while (!request.isDone)
            {
                Debug.Log(request.progress);
                yield return null;
            }


            if (uwr.isNetworkError || uwr.isHttpError)
            {
                Debug.Log(uwr.error);
            }
            else
            {
                // Get downloaded asset bundle
                AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(uwr);

                assetBundleInstance = Instantiate(bundle.LoadAsset(assetName)) as GameObject;
                assetBundleInstance.transform.position = transform.position;
                assetBundleInstance.transform.localScale = new Vector3(.08f, .08f, .08f);
                assetBundleInstance.transform.SetParent(transform);
                contador.text = "Descargado: " + assetName + "\n" + bundleURL;
            }
        }
    }

回答1:

i need to capture a WebRequestAsyncOperation object to find the progress but i cannot find it

If you mean that WebRequestAsyncOperation is not the same as UnityWebRequestAsyncOperation, turns out they are.


UnityWebRequestAsyncOperation

"Asynchronous operation object returned from UnityWebRequest.SendWebRequest()."

Which is the method you already are using.

Source: https://docs.unity3d.com/ScriptReference/Networking.UnityWebRequestAsyncOperation.html

I tried using AsyncOperation and UnityWebRequestAsyncOperation and my routine works with both, what is the difference of using one or another?

UnityWebRequestAsyncOperation inherits from AsyncOperation, meaning they share the same fields and likely also same methods. UnityWebRequestAsyncOperation additionally has the field below though:

webRequest Returns the associated UnityWebRequest that created the operation.

If this didn't answer your question please elaborate.