广告不会显示网络何时不可用。 所以我想要一个脚本或其他方法,可以使应用程序停止,除非网络访问可用。
Ads wont show when the network is unavaliable. So I want a script or other method which can make the app stop unless network access is available.
最满意答案
幸运的是在Unity中就是这么简单,
Android的
iOS版
打开“构建设置”对话框(文件菜单)
单击PlayerSettings,查看Inspector,到OtherSettings面板,
向下滚动到InternetAccess ...
...并选择你想要的模式!
而已。
高级问题:请注意,这个非常有用的电话......
http://docs.unity3d.com/ScriptReference/Application-internetReachability.html
然而,它涉及使用; 这是实现这一目标的重大项目。 在更复杂的项目中,还要考虑
http://docs.unity3d.com/ScriptReference/NetworkReachability.html
如果您想了解这些,请从在线发布的许多入门示例开始,例如https://stackoverflow.com/a/24351716/294884 (该人明智地将谷歌着名的DNS用作“您几乎可以随时使用的网站”从理论上讲,如果互联网存在“ - 当你试图实现这个时,只是处理许多问题中的一个)。
最后,请注意,从某种意义上说,一种方法是这样做
只是去互联网找一些你知道的页面
如果,等待了足够的时间(这将是多长时间?可能是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. }Fortunately in Unity it is this simple,
Android
iOS
Open the Build Settings dialog (file menu)
Click PlayerSettings, look at Inspector, to to OtherSettings panel,
scroll down to InternetAccess ...
... and choose the mode you want!
That's it.
Advanced issues: Note too, this very useful call ...
http://docs.unity3d.com/ScriptReference/Application-internetReachability.html
which however is quite involved to use; it's a major project to implement this. In more complex projects, consider also
http://docs.unity3d.com/ScriptReference/NetworkReachability.html
If you want to get in to these, start off with the many starter examples posted online, for example https://stackoverflow.com/a/24351716/294884 (that person sensibly uses google's famous DNS as a "site you can almost always reach in theory if the internet is in existence" - just one of many problems to deal with when you try to implement this).
Finally, note that in a sense one way to do this is
just go to the internet for some page you know is there
if, after waiting a sufficient amount of time (how long would that be? maybe 5 seconds - I don't know) you don't get a result, then assume there's no decent internet connectivity.
Here's the actual code to get you going on that if you're never done a WWW before in Unity.
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. }更多推荐
发布评论