I am using 2021.3.24f1 release and when I test my offline flow in the unity editor on the same Windows 10 machine, it works as expected.
ie.
Application.internetReachability == NetworkReachability.NotReachable is True when WiFi is disconnected or ethernet cable is unplugged.
However the same test does not work in the Windows standalone build generated and run of the same machine.
I reviewed this thread which is relevant to UWP but the workaround does not work for me
Is this a bug or is there another way to verify internet connectivity in the Windows standalone build?
Please do not use this to test actual connection!!!
This property is mostly useful on handhelds to distinguish fast and cheap WiFi connection from carrier networking.
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. Non-handhelds are considered to always be capable of NetworkReachability.ReachableViaLocalAreaNetwork.
Note that I do not inherit from MonoBehaviour so I use a utility class GlobalCoroutineHost.
Check this out for help
To use the class call
//call this at the start of the application once
CheckInternetConnectivity.Init();
//to check for internet call this
CheckInternetConnectivity.IsDeviceConnectedToInternet()
public static class CheckInternetConnectivity
{
/***
* In window standalone build Application.InternetReachability does not work.
* However, it works well enough in Android.
* Based on suggestions from this thread
* https://stackoverflow.com/questions/2031824/what-is-the-best-way-to-check-for-internet-connectivity-using-net *
* Following has been implemented
* */
private static volatile bool internetConnected;
private const int RECHECK_DELAY_SECONDS = 10;
private static string weburl = "www.google.com";
public static void Init()
{
internetConnected = false;
//initate a periodic check only for non handheld devices
#if !(UNITY_ANDROID || UNITY_IOS || UNITY_TVOS)
weburl = CultureInfo.InstalledUICulture switch
{
{ Name: var n } when n.StartsWith("fa") => // Iran
"http://www.aparat.com",
{ Name: var n } when n.StartsWith("zh") => // China
"http://www.baidu.com",
_ =>
"http://www.google.com",
};
//intiate the first check
GlobalCoroutineHost.StartRoutine(CheckWebConnectivity(new object(), 0));
#endif
}
public static bool IsDeviceConnectedToInternet()
{
#if UNITY_ANDROID || UNITY_IOS || UNITY_TVOS
return Application.internetReachability != NetworkReachability.NotReachable
#else
return internetConnected;
#endif
}
/// <summary>
/// https://stackoverflow.com/a/23009713
/// This approach has been discussed in this response
/// </summary>
private static IEnumerator CheckWebConnectivity(object state, int delayinSeconds)
{
yield return null;
//wait if requested
if(delayinSeconds > 0)
{
yield return new WaitForSeconds(delayinSeconds);
}
if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
{
using (WebClient webClient = new WebClient())
{
webClient.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.BypassCache);
webClient.Proxy = null;
webClient.OpenReadCompleted += webClient_OpenReadCompleted;
webClient.OpenReadAsync(new Uri(weburl));
}
}
}
private static void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error == null)
{
internetConnected = true;
}
else
{
internetConnected = false;
}
//reschedule the check in a few seconds
GlobalCoroutineHost.StartRoutine(CheckWebConnectivity(sender, RECHECK_DELAY_SECONDS));
}
}