Как получить прогресс загрузки с помощью UnityWebRequest для загрузки Asset Bundle с сервера? - PullRequest
0 голосов
/ 23 мая 2019

Я все еще использую UnityWebRequest для загрузки и загрузки пакета ресурсов из контейнера сервера.Проблема заключается в том, что значение для прогресса загрузки всегда равно 0. Как я могу получить значение для процесса загрузки?

Код Ниже указано, что я пытаюсь загрузить и получить прогресс загрузки.

//Method to download the assetbundle
IEnumerator DownloadAsset()
{
    string url = here the URL for asset bundle;
    using (var uwr = new UnityWebRequest(url, UnityWebRequest.kHttpVerbGET))
    {
        uwr.downloadHandler = new DownloadHandlerAssetBundle(url, 36, 0);
        UnityWebRequestAsyncOperation operation = uwr.SendWebRequest();
        yield return StartCoroutine(DownloadProgress(operation));

        AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(uwr);
        {
            print("Get asset from bundle...");
        }


        //Load scene
        uwr.Dispose();
        print("ready to Load scene from asset...");
        StartCoroutine(LoadSceneProgress("Example"));
        bundle.Unload(false);
    }
}

//Method for download progress
IEnumerator DownloadProgress(UnityWebRequestAsyncOperation operation)
{
    while (!operation.isDone)
    {
        progressBar.color = Color.red;
        downloadDataProgress = operation.progress * 100;
        progressBar.fillAmount = downloadDataProgress / 100;
        print("Download: " + downloadDataProgress);
        yield return null;
    }
    Debug.Log("Done");
}

Я ожидаю, что на экране отобразится индикатор загрузки или процент загрузки.но значение прогресса загрузки всегда 0.

1 Ответ

0 голосов
/ 23 мая 2019

Вместо

yield return StartCoroutine(DownloadProgress(operation));

правильный способ yield использования IEnumerator просто

yield return DownloadProgress(operation);

Однако почему бы просто не сделать это напрямую в том же самомCoroutine?

Однако я бы порекомендовал использовать UnityWebRequestAssetBundle.GetAssetBundle вместо того, чтобы конфигурировать его с нуля и некоторые другие изменения:

IEnumerator DownloadAsset()
{
    string url = "<here the URL for asset bundle>";

    /*
     * directly use UnityWebRequestAssetBundle.GetAssetBundle
     * instead of "manually" configure and attach the download handler etc
     */
    using (var uwr = new UnityWebRequestAssetBundle.GetAssetBundle(url, 36, 0)
    {
        var operation = uwr.SendWebRequest();

        /* 
         * this should be done only once actually 
         */
        progressBar.color = Color.red;

        while (!operation.isDone)
        {
            /* 
             * as BugFinder metnioned in the comments
             * what you want to track is uwr.downloadProgress
             */
            downloadDataProgress = uwr.downloadProgress * 100;

            /*
             * use a float division here 
             * I don't know what type downloadDataProgress is
             * but if it is an int than you will always get 
             * an int division <somethingSmallerThan100>/100 = 0
             */
            progressBar.fillAmount = downloadDataProgress / 100.0f;

            print("Download: " + downloadDataProgress);
            yield return null;
        }

        AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(uwr);
        {
            print("Get asset from bundle...");
        }

        /* 
         * You do not have to Dispose uwr since the using block does this automatically 
         */
        //uwr.Dispose();

        //Load scene
        print("ready to Load scene from asset...");
        StartCoroutine(LoadSceneProgress("Example"));
        bundle.Unload(false);
    }
}
...