Check for internet connectivity from Unity

后端 未结 6 1962
轮回少年
轮回少年 2020-12-07 00:41

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 di

相关标签:
6条回答
  • 2020-12-07 01:04

    You can check network connection using this code

    if(Application.internetReachability == NetworkReachability.NotReachable){
           Debug.Log("Error. Check internet connection!");
    }
    
    0 讨论(0)
  • 2020-12-07 01:16

    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.

    0 讨论(0)
  • 2020-12-07 01:23

    i know this is old, but maybe can help you.

    public bool CheckInternetConnection(){
        return !(Application.internetReachability == NetworkReachability.NotReachable)
    }
    
    0 讨论(0)
  • 2020-12-07 01:23
    void Start()
    {
        StartCoroutine(CheckInternetConnection(isConnected =>
        {
            if (isConnected)
            {
                Debug.Log("Internet Available!");
            }
            else
            {
                Debug.Log("Internet Not Available");
            }
        }));
    }
    
    IEnumerator CheckInternetConnection(Action<bool> action)
    {
        UnityWebRequest request = new UnityWebRequest("http://google.com");
        yield return request.SendWebRequest();
        if (request.error != null) {
            Debug.Log ("Error");
            action (false);
        } else{
            Debug.Log ("Success");
            action (true);
        }
    }
    

    Since WWW has deprecated, UnityWebRequest can be used instead.

    0 讨论(0)
  • 2020-12-07 01:28
        public static IEnumerator CheckInternetConnection(Action<bool> syncResult)
        {
            const string echoServer = "http://google.com";
    
            bool result;
            using (var request = UnityWebRequest.Head(echoServer))
            {
                request.timeout = 5;
                yield return request.SendWebRequest();
                result = !request.isNetworkError && !request.isHttpError && request.responseCode == 200;
            }
            syncResult(result);
        }
    
    0 讨论(0)
  • 2020-12-07 01:29

    I've created an Android's library that can be used as Unity's plugin for this purpose. If anyone's interested it's available under https://github.com/rixment/awu-plugin

    As for the iOS version unfortunately I don't have enough of knowledge in this area. It would be nice if someone could extend the repo to include the iOS version too.

    0 讨论(0)
提交回复
热议问题