UnityWebRequest.SendWebRequest不在手机上发送 [英] UnityWebRequest.SendWebRequest doens't send on mobile

查看:277
本文介绍了UnityWebRequest.SendWebRequest不在手机上发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用HTTPS中的UnitywebRequest方法下载Assetbundles,但是UnityWebRequest.SendWebRequest似乎花了很多时间才能真正开始接收任何数据.

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);将随机返回0时间(有时从几分钟到几分钟不等).但是www. isNetworkError从未被使用过,一旦开始接收字节,它将在几毫秒内下载整个文件.

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.

以前,我在http服务器上使用了完全相同的脚本,它可以完美运行而没有任何延迟,但是一旦我切换到https,它就会花一些时间.延迟也不会在编辑器中发生(Unity版本2017.2.1f1运行时版本.net 3.5具有2.0子集api兼容性),但会在我的所有移动设备上发生(Oneplus 3,Samsung galaxy s8,samsung galaxy s6).

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).

起初,www.responseCode返回了301 Moved Permanently,我通过使用新的https url而不是http url来解决此问题,希望可以解决此问题.但是没有,现在我只得到200 OK.

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)?

解决方法 我通过使用WWW类而不是UnityWebRequest解决了这个问题.现在,延迟已完全消失,但是由于Unity似乎默认拒绝了SSL证书,因此未进行SSL证书验证.我不会真正将其称为修复程序,但现在可以使用.

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.

推荐答案

UnityWebRequest总是很麻烦-有时UnityWebRequests没有任何意义...

Always trouble with UnityWebRequest - sometimes nothing makes sense with UnityWebRequests...

您可以尝试替换

 UnityWebRequest www = new UnityWebRequest(FileManager.RequestFile(path));

使用

 UnityWebRequest www = new UnityWebRequestAssetBundle(FileManager.RequestFile(path), 0);

可选的测试:

我还认为您的while循环不是保存"来捕获网络错误. 我希望使用文档

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
}

希望这会有所帮助.

这篇关于UnityWebRequest.SendWebRequest不在手机上发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆