Two type of devices are their when concerning about Network Connection,
There are various aspects need to keep in mind when checking for network connection in unity3d development environment.
Unity provides various classes to do so, some of those are listed below with best suitable example for that, hope this might get some relief..
using System.Collections;
public class NetworkManager : MonoBehaviour
{
public static bool IsInternetConnection()
{
bool isConnectedToInternet = false;
if(Application.internetReachability == NetworkReachability.ReachableViaCarrierDataNetwork ||
Application.internetReachability == NetworkReachability.ReachableViaLocalAreaNetwork)
{
isConnectedToInternet = true;
}
return isConnectedToInternet;
}
}
{
- Hand Held Devices - iPhone, iPad, Android, Windows Phone, Tablets etc.
- Non Hand Held Devices - PC, Mac, Linux etc
There are various aspects need to keep in mind when checking for network connection in unity3d development environment.
Unity provides various classes to do so, some of those are listed below with best suitable example for that, hope this might get some relief..
Application.internetReachability can be use to check current reachable type, this can be use on all kind of device including hand held devices, this returns a member of an enum NetworkRechability, Bare in mind that this only checks all possible network connection on device but not guaranteed that device is connected to the internet.
This can be use to distinguish whether device is connected via high speed wifi/local area network or using packetdata/career data network
Below are all network reachability options that NetworkReachability enum have,
- ReachableViaCareerDataNetwork
- ReachableViaLocalAreaNetwork
- NotReachable
Usage Example:
using UnityEngine;using System.Collections;
public class NetworkManager : MonoBehaviour
{
public static bool IsInternetConnection()
{
bool isConnectedToInternet = false;
if(Application.internetReachability == NetworkReachability.ReachableViaCarrierDataNetwork ||
Application.internetReachability == NetworkReachability.ReachableViaLocalAreaNetwork)
{
isConnectedToInternet = true;
}
return isConnectedToInternet;
}
}
To check for Internet Connection,
IEnumerator CheckForConnection(){
Ping png = new Ping("139.130.4.5");
float startTime = Time.time;
while (Time.time < startTime + 5.0f)
{
yield return new WaitForSeconds(0.1f);
}
if(png .isDone)
{
print("Connected!");
}
else
{
print("Not Connected!");
}
}
float startTime = Time.time;
while (Time.time < startTime + 5.0f)
{
yield return new WaitForSeconds(0.1f);
}
if(png .isDone)
{
print("Connected!");
}
else
{
print("Not Connected!");
}
}