Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WWW在没有internet时冻结_C#_Unity3d_Httprequest - Fatal编程技术网

C# WWW在没有internet时冻结

C# WWW在没有internet时冻结,c#,unity3d,httprequest,C#,Unity3d,Httprequest,我正在Unity中编写一个简单的代码,以检查我是否能够通过我的应用程序访问网站。这是我写的代码: IEnumerator检查互联网 { WWW.wwinternet=新建WWWhttp://google.com; 净收益率; 如果wwwInternet.bytesDownloaded==0 { //返回新的WaitForSeconds1f; Debug.log未连接到Internet; } 其他的 { Debug.log连接到Internet; internetMenu.SetActivefal

我正在Unity中编写一个简单的代码,以检查我是否能够通过我的应用程序访问网站。这是我写的代码:

IEnumerator检查互联网 { WWW.wwinternet=新建WWWhttp://google.com; 净收益率; 如果wwwInternet.bytesDownloaded==0 { //返回新的WaitForSeconds1f; Debug.log未连接到Internet; } 其他的 { Debug.log连接到Internet; internetMenu.SetActivefalse; } } 我发现了一个bug,如果我在打开互联网的情况下运行此应用程序,它会显示已连接,但当我关闭互联网并立即运行应用程序时,它不会记录任何内容。仅当我下次重新启动应用程序时,它才会显示“未连接”。 有人知道为什么第一次什么都不记录吗?谢谢

这是WWW类的一个bug,在这里已经存在很长时间了。每个设备的行为可能都不同。如果Wifi被禁用,它会在编辑器上冻结。快速测试表明此错误尚未修复

您需要使用HttpWebRequest而不是WWW

在下面的示例中,线程用于避免请求阻塞Unity程序,UnityThread用于在请求完成时回调Unity主线程。从邮件中读取

它的使用非常简单:

void Start()
{
    isOnline((online) =>
    {
        if (online)
        {
            Debug.Log("Connected to Internet");
            //internetMenu.SetActive(false);
        }
        else
        {
            Debug.Log("Not Connected to Internet");
        }
    });
}

有多少次显示已连接?只有一次实际连接代码只输入一次。。。你需要更频繁地调用它,放置一个计时器并调用它。你使用的是哪个版本?对于2017.3.0f3,我无法重现错误,代码运行良好。我使用的是相同的版本。如果我使用飞行模式打开/关闭互联网,这有关系吗?
public static class ExtensionMethods
{
    public static void changeSysTemHeader(this HttpWebRequest request, string key, string value)
    {
        WebHeaderCollection wHeader = new WebHeaderCollection();
        wHeader[key] = value;

        FieldInfo fildInfo = request.GetType().GetField("webHeaders",
                                                System.Reflection.BindingFlags.NonPublic
                                                   | System.Reflection.BindingFlags.Instance
                                                   | System.Reflection.BindingFlags.GetField);

        fildInfo.SetValue(request, wHeader);
    }
}
void Start()
{
    isOnline((online) =>
    {
        if (online)
        {
            Debug.Log("Connected to Internet");
            //internetMenu.SetActive(false);
        }
        else
        {
            Debug.Log("Not Connected to Internet");
        }
    });
}