Checking Internet Connection in Unity

Unity Internet Connection Check


Checking Internet Connection in Unity

 Hello to you all, friends! In this post, I will explain how you can check whether the player's device is connected to the internet in your games. Of course, while doing this, I always try to explain the logic as much as I can—anyway, without further ado, let's get back to our topic.

1st Method

As the first method, I will talk about a method already provided by Unity.

There is a function in Unity called "Application.internetReachability" which allows us to check for an internet connection, but this method is not actually recommended. Now you'll ask me, "Why isn't it recommended?" Let me first show you why Unity does not recommend it.

Note: Do not use this property to determine the actual connectivity. E.g. the device can be connected to a hot spot, but not have the actual route to the network.

Unity gives us such a warning for this method. If we translate it into Turkish, it can be partly translated as follows.

Note: Do not use this method to detect a real connection. (By real connection, it means to differentiate between virtual networks and networks with internet access.) Devices may be connected to a virtual network and may not actually have internet access. (For the parts I refer to as virtual networks, I recommend you research the concept of hotspot internet.)

To summarize, this method is not recommended because it can perceive there is internet connection even if the device's WiFi has no internet. (What kind of expression is that...)

Let's explain this method right away with an example for those who want to use it.

using UnityEngine;

public class InternetKontrol : MonoBehaviour
{


    private void Awake()
    {
        InternetKontrolEt();
    }
    void InternetKontrolEt()
    {
        if (Application.internetReachability == NetworkReachability.NotReachable)
        {
           // No internet
        }
        else if (Application.internetReachability == NetworkReachability.ReachableViaCarrierDataNetwork)
        {
           // There is an internet connection
        }
        //Check if the device can reach the internet via a LAN
        else if (Application.internetReachability == NetworkReachability.ReachableViaLocalAreaNetwork)
        {
           // There is a LAN connection
        }
    }
}

Those who want to use this not recommended method can implement and use it in their project as above. Now let's move on to our recommended method.

2nd Method

This method is much more accurate than the previous one and is the method I recommend using in your projects to be published. In this method, we send a request (Ping) to any website, and if a response is received from that site confirming the request reached it, it means the internet connection check is successful; however, if there is no response, it means there is no internet. This method gives more accurate results compared to the 1st method above.

Now that we've explained the logic of this method, let's see how it's done.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class InternetBaglantisiKontrol : MonoBehaviour
{
    private bool internetVarmi = false; // we create a bool variable to use in the internet check

    private void Awake()
    {
        StartCoroutine(InternetKontrol("https://www.google.com")); // we call the internet check function
    }

    IEnumerator InternetKontrol(string url)
    {
        using (UnityWebRequest webIstek = UnityWebRequest.Get(url)) // we send a request to the website, you can think of this like the ping command in cmd.
        {
            yield return webIstek.SendWebRequest(); // we wait for the request to be sent
            if (webIstek.isNetworkError) // if the request is negative, i.e. there is no connection
            {
                Debug.Log("No internet connection.");
                internetVarmi = false; // if there is no internet, we set the variable as false
            }
            else // if there is a connection
            {
                Debug.Log("Internet connection exists.");
                internetVarmi = true; // if there is internet, we set the variable as true
            }
        }
    }
}

Yes, friends, as you can see, with this method, we can check for an internet connection more accurately.

No connection

Connection exists

Checking internet connection with Unity is that simple. If you have any questions in your mind or a point you are stuck on regarding this topic, feel free to write in the comment section below. Have a nice day..