“Gameobject not being set active in ‘catch’. I’ve checked everything.”

huangapple go评论70阅读模式
英文:

Gameobject not being set active in 'catch'. I've checked everything

问题

以下是我要翻译的部分:

public GameObject NoInternetConnectionPanel;

private void Start()
{
    // 检查互联网连接
    InvokeRepeating("CheckInternetConnection", 0f, 1f);
}

private void CheckInternetConnection()
{
    var request = WebRequest.Create("http://google.com");
    request.Method = "HEAD";

    request.BeginGetResponse(result =>
    {
        try
        {
            using (var response = request.EndGetResponse(result) as HttpWebResponse)
            {
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    Debug.Log("互联网连接可用");
                }
                /*else
                {
                    Debug.Log("没有互联网连接");
                }*/
            }
        }
        catch (WebException ex)
        {
            Debug.Log("没有互联网连接");
            NoInternetConnectionPanel.SetActive(true);
        }
    }, null);
    
    StartCoroutine(WaitAndCheck());
}

private IEnumerator WaitAndCheck()
{
    yield return new WaitForSeconds(1f);  
}

如果你有任何其他问题或需要进一步帮助,请随时提出。

英文:

Here is what I'm trying to do:

Trying to check if there's internet connection or no. If there is no internet connection, I want to set active a panel showing 'no internet connection'.

I dragged the panel to the inspector. The script which sets this panel active is on a gameobject which is active in hierarchy.

Here is the code:

public GameObject NoInternetConnectionPanel;

private void Start()
{
    //Check Internet Connection
    InvokeRepeating("CheckInternetConnection", 0f, 1f);
}

private void CheckInternetConnection()
{
    var request = WebRequest.Create("http://google.com");
    request.Method = "HEAD";

    request.BeginGetResponse(result =>
    {
        try
       {
            using (var response = request.EndGetResponse(result) as HttpWebResponse)
            {
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    Debug.Log("Internet connection available");
                }
                /*else
                {
                    Debug.Log("No internet connection");
                }*/
            }
        }
        catch (WebException ex)
        {
            Debug.Log("No internet connection");
            NoInternetConnectionPanel.SetActive(true);
        }
    }, null);
    
    StartCoroutine(WaitAndCheck());
}
private IEnumerator WaitAndCheck()
{
    yield return new WaitForSeconds(1f);  
}

The debug log message "no internet connection" is being shown in console. But the 'NoInternetConnectionPanel.SetActive(true)' is not working.

What could be the issue?

答案1

得分: 1

这很可能是因为它正在另一个不受大多数Unity API支持的线程上执行。通常情况下,你不应该使用 WebRequest,而应该在 Coroutine 中使用 UnityWebRequest

此外,请注意你的 WaitAndCheck 例程实际上没有做任何事情。

我更愿意使用类似以下的方法:

public GameObject NoInternetConnectionPanel;

private IEnumerator Start()
{
    while (true)
    {
        yield return CheckInternetConnection();
        yield return new WaitForSeconds(1f);
    }
}

private IEnumerator CheckInternetConnection()
{
    using (var request = UnityWebRequest.Head("http://google.com"))
    {
        yield return request.SendWebRequest();

        var connection = request.result == UnityWebRequest.Result.Success;

        NoInternetConnectionPanel.SetActive(!connection);

        if (connection)
        {
            Debug.Log("Internet connection available");
        }
        else
        {
            Debug.LogWarning("No internet connection");
        }
    }
}

除了完全不使用网络请求,你还可以使用一个简单的 Ping,通常与例如 8.8.8.8,谷歌的全球DNS服务器一起使用。

private IEnumerator CheckInternetConnection()
{
    var ping = new Ping("8.8.8.8");
    var maxPingTime = 1f; // 在宣布 ping 失败之前等待的最长时间
    var connection = false;
    for (var timePassed = 0f; timePassed <= maxPingTime; timePassed += Time.deltaTime)
    {
        if (ping.isDone)
        {
            connection = true;
            break;
        }
    }

    NoInternetConnectionPanel.SetActive(!connection);

    if (connection)
    {
        Debug.Log("Internet connection available");
    }
    else
    {
        Debug.LogWarning("No internet connection");
    }
}
英文:

That's most probably because it is being executed on another any thread which is not supported by most of the Unity API. In general you shouldn't be using WebRequest at all but rather use UnityWebRequest in a Coroutine.

Also not btw that your WaitAndCheck routine doesn't do anything at all.

I would rather use something like e.g.

public GameObject NoInternetConnectionPanel;

private IEnumerator Start()
{
    while(true)
    {
        yield return CheckInternetConnection();
        yield return new WaitForSeconds(1f);
    }
}

private IEnumerator CheckInternetConnection()
{
    using(var request = UnityWebRequest.Head(&quot;http://google.com&quot;))
    {
        yield return request.SendWebRequest();

        var connection = request.result == UnityWebRequest.Result.Success;

        NoInternetConnectionPanel.SetActive(! connection);

        if(connection)
        {
            Debug.Log(&quot;Internet connection available&quot;);      
        }
        else
        {
            Debug.LogWarning(&quot;No internet connection&quot;);
        }
    }
}

Instead of using a we request at all you can also use a simple Ping which is often used with e.g. 8.8.8.8, Google's global DNS server.

private IEnumerator CheckInternetConnection()
{
    var ping = new Ping(&quot;8.8.8.8&quot;);
    var maxPingTime = 1f; // maximum time to wait before declaring the ping as failed
    var connection = false;
    for(var timePassed = 0f; timePassed &lt;= maxPingTime; timePassed += Time.deltaTime)
    {
        if(ping.isDone)
        {
            connection = true;
            break;
        }
    }

    NoInternetConnectionPanel.SetActive(! connection);

    if(connection)
    {
        Debug.Log(&quot;Internet connection available&quot;);      
    }
    else
    {
        Debug.LogWarning(&quot;No internet connection&quot;);
    }
}

huangapple
  • 本文由 发表于 2023年2月19日 11:39:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75497835.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定