Checking Internet Connection in Unity
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
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
}
}
}
}


Yorum Gönder