gpt4 book ai didi

c# - 如何使用 UnityWebRequest 从服务器下载 Asset Bundle 获取下载进度?

转载 作者:行者123 更新时间:2023-12-05 01:15:24 27 4
gpt4 key购买 nike

我还是新手,使用 UnityWebRequest 从服务器容器下载和加载 Assets 包。问题是下载进度的值始终为 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。

最佳答案

代替

yield return StartCoroutine(DownloadProgress(operation));

yieldIEnumerator 的正确方法很简单

yield return DownloadProgress(operation);

但是为什么不直接在同一个协程中做呢?

然而,我建议宁愿使用 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);
}
}

Mayur Asodariya 的注释来自他的以下评论:

您的服务器可能不提供下载大小,因此不提供进度信息。在这种情况下,您可以关注 this post正确配置您的服务器。

关于c# - 如何使用 UnityWebRequest 从服务器下载 Asset Bundle 获取下载进度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56270806/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com