如果网络可用,我怎样才能使Unity应用程序正常工作(How can I make a Unity app work, only if network is avaliable)

编程入门 行业动态 更新时间:2024-10-28 19:26:16
如果网络可用,我怎样才能使Unity应用程序正常工作(How can I make a Unity app work, only if network is avaliable)

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

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

enter image description here

iOS

enter image description here

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

更多推荐

本文发布于:2023-07-31 15:01:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1345310.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:能使   应用程序   工作   网络   network

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!