如果网络可用,我怎样才能使Unity应用程序正常工作

时间:2016-02-04 14:04:11

标签: unity3d

广告不会显示网络何时无法使用。所以我想要一个脚本或其他方法,可以让应用程序停止,除非网络访问可用。

1 个答案:

答案 0 :(得分:4)

幸运的是在Unity中,这很简单,

<强>的Android

enter image description here

<强>的iOS

enter image description here

打开“构建设置”对话框(文件菜单)

单击“PlayerSettings”,查看“检查器”,“到OtherSettings”面板,

向下滚动到InternetAccess ...

...然后选择你想要的模式!

那就是它。

高级问题:请注意,这个非常有用的电话......

http://docs.unity3d.com/ScriptReference/Application-internetReachability.html

然而,它非常涉及使用;它是实现这一目标的重要项目。在更复杂的项目中,还要考虑

http://docs.unity3d.com/ScriptReference/NetworkReachability.html

如果您想了解这些内容,请从在线发布的众多入门示例开始,例如https://stackoverflow.com/a/24351716/294884(该人明智地使用谷歌着名的DNS作为&#34;网站,您可以如果互联网存在的话,理论上几乎总是达到目标 - 当你试图实现这个时,只是处理许多问题之一。)

最后,请注意,从某种意义上说,一种方法是

  • 只需访问互联网,查看您知道的页面

  • 等待足够长的时间之后 (这会是多长时间?也许是5秒钟 - 我不知道) 你没有得到结果,然后假设没有 体面的互联网连接。

如果您以前从未在Unity中完成WWW,那么这里有实际的代码可以让您继续。

private string testUrl = "http://someTestSite.com/whatever.html";

private void CheckIfWeCanGetNetPagesAtAll()
    {
    StartCoroutine(_Check());
    }

private IEnumerator _Check()
    {
    WWW w = new WWW( testUrl.URLAntiCacheRandomizer() );
    yield return w;
    if (w.error != null)
        {
        Debug.Log(">>> could not get internet test page, Error .. " +w.error);
        // bring up error message for user, that the app canot be used
        // just no because you have no internet access.
        }
     else
        {
        // no problems
        }

    // not shown here ... you may prefer to implement your own time-out
    // rather than just waiting for WWW to time-out, which can be long.
    }