问题
I am downloading Assetbundles using the UnitywebRequest method from HTTPS, however the UnityWebRequest.SendWebRequest seems to take its sweet time to actually start receiving any data.
public static IEnumerator DownloadMenuAssetBundle(string fileName)
{
string path = Path.Combine(Globals.Platform, fileName);
UnityWebRequest www = new UnityWebRequest(FileManager.RequestFile(path));//this returns the complete url to the bundle e.g https://mywebsite.com/unity/myBundle
www.downloadHandler = new DownloadHandlerBuffer();
www.SendWebRequest();
while (!www.isDone)
{
Debug.Log("response: " + www.responseCode);
if (www.isNetworkError)
{
Debug.Log(www.error);
}
Debug.Log("downloaded:" + www.downloadedBytes);
yield return null;
}
Debug.Log("downloaded bytes: " + www.downloadedBytes);
Debug.Log("final response:" + www.responseCode);
if (www.error != null)
{
Debug.LogError("Encountered error while downloading: <color=blue>" + fileName + "</color>: " + www.error);
}
else
{
//rest of the logic, this works
}
Debug.Log("response: " + www.downloadedBytes);
will return 0
for a random amount of time (ranging from a few to up to a couple minutes at times). But www. isNetworkError
is never hit, and once bytes do start to be received it'll download the entire thing in a couple of miliseconds.
Previously i was using the exact same script on a http server and it worked flawlessly without any delays, but once i switched over to https it started taking a while. The delay also does not happen in the editor (Unity version 2017.2.1f1 runtime version .net 3.5 with 2.0 subset api compability) but happens on all my mobile devices (Oneplus 3, samsung galaxy s8, samsung galaxy s6).
At first www.responseCode
returned a 301 Moved Permanently
, i resolved this by using the new https url instead of the http url hoping this would fix it. However it didn't and now i only get 200 OK
.
It is also an inconsistent issue because the timing it takes isn't ever the same, neither does it even happen all the time (but does most of the time)
Can this be an issue by the security layer taking additional time or the server taking its time to respond? if this is the issue how would i be able to track this down (though i doubt this as it works well in the editor)?
EDIT: workaround
I got around the issue by using a WWW
class instead of UnityWebRequest
. the delay is completely gone now but no SSL certificate validation is being done as Unity seems to deny them by default. I wouldn't really call it a fix but works for now.
回答1:
Always trouble with UnityWebRequest
- sometimes nothing makes sense with UnityWebRequests...
You can try to replace
UnityWebRequest www = new UnityWebRequest(FileManager.RequestFile(path));
with
UnityWebRequest www = new UnityWebRequestAssetBundle(FileManager.RequestFile(path), 0);
Optional for testing:
Also I think your while
loop is not "save" to catch the network error.
I would prefer the following like mentioned in the documentation
UnityWebRequest www = new UnityWebRequestAssetBundle(FileManager.RequestFile(path);
yield return www.SendWebRequest();
if (request.isNetworkError || request.isHttpError)
{
Debug.Log(www.error.ToString());
}
else
{
// for testing only // if yield return www.SendWebRequest(); is working as expected www.isDone = true here!
while (!www.isDone)
{
Debug.Log("Something is wrong! " + www.responseCode);
yield return new WaitForSeconds(0.1f);
}
// do whatever you want
}
Hope this helps.
来源:https://stackoverflow.com/questions/52588303/unitywebrequest-sendwebrequest-doenst-send-on-mobile