检查来自 Unity 的互联网连接 [英] Check for internet connectivity from Unity

查看:22
本文介绍了检查来自 Unity 的互联网连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个为 Android 和 iOS 平台构建的 Unity 项目.我想检查桌面、Android 和 iOS 设备上的互联网连接.我已经阅读了三种不同的解决方案:

  1. Ping 某些东西(例如 Google) - 我完全不喜欢这样的决定,而且我已经阅读了有关 Android 错误的文章.

  2. Application.internetReachability - 根据 Unity 的文档,这个函数只会确定我有可能连接到互联网(它不保证真正的连接).p>

  3. Network.TestConnection() - 如果我没有互联网连接,我的应用程序就会失败.所以这也不正确.

如何从 Unity 中确定我是否具有 Internet 连接?

解决方案

我实际上并不认为 Network.TestConnection() 是适合这项工作的工具.根据文档,在我看来,它用于测试 NAT 是否为正常工作,并且您的客户端可通过 IP 公开访问,但您要检查的是您是否具有一般的互联网连接.

这是我在 Unity Answers 上找到的 解决方案,由用户 pixel_fiend,它只是测试网站以查看用户是否具有连接性.此代码的一个好处是它使用 IEnumerator 进行异步操作,因此连接测试不会阻止应用程序的其余部分:

IEnumerator checkInternetConnection(Action action){WWW www = new WWW("http://google.com");收益率返回 www;如果(www.error != null){动作(假);} 别的 {行动(真);}}无效开始(){StartCoroutine(checkInternetConnection((isConnected)=>{//这里处理连接状态}));}

您可以将网站更改为您想要的任何内容,甚至可以修改代码以在可以访问多个站点中的任何一个时返回成功.AFAIK 如果不尝试连接到互联网上的特定站点,就无法检查真正的互联网连接,因此ping"一个或多个这样的网站可能是确定连接的最佳选择.

I have a Unity project which I build for Android and iOS platforms. I want to check for internet connectivity on Desktop, Android, and iOS devices. I've read about three different solutions:

  1. Ping something (for example Google) - I totally dislike such decision, and I've read about mistakes on Android.

  2. Application.internetReachability - According to Unity's documentation, this function will only determine that I have a POSSIBILITY of connecting to the Internet (it doesn't guarantee a real connection).

  3. Network.TestConnection() - If I have no Internet connection, my application fails. So this isn't correct either.

How can I determine whether I have internet connectivity from within Unity?

解决方案

I don't actually believe that Network.TestConnection() is the right tool for this job. According to the documentation, it looks to me like it's meant for testing if NAT is working and your client is publicly reachable by IP, but what you want to check for is whether you have general internet connectivity.

Here is a solution that I found on Unity Answers by user pixel_fiend, which simply tests a website to see if the user has connectivity. One benefit of this code is that it uses IEnumerator for asynchronous operation, so the connectivity test won't hold up the rest of your application:

IEnumerator checkInternetConnection(Action<bool> action){
     WWW www = new WWW("http://google.com");
     yield return www;
     if (www.error != null) {
         action (false);
     } else {
         action (true);
     }
 } 
 void Start(){
     StartCoroutine(checkInternetConnection((isConnected)=>{
         // handle connection status here
     }));
 }

You can change the website to whatever you want, or even modify the code to return success if any one of a number of sites are reachable. AFAIK there is no way to check for true internet connectivity without trying to connect to a specific site on the internet, so "pinging" one or more websites like this is likely to be your best bet at determining connectivity.

这篇关于检查来自 Unity 的互联网连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆