Wednesday, April 22, 2015

Check for Network Connection Unity3d

Two type of devices are their when concerning about Network Connection,

  • 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!");
     }
}

3 comments:

  1. Hi!

    UnityEngine.Ping not available on windows-phone.

    "The type or namespace name `Ping' could not be found. Are you missing a using directive or an assembly reference?"

    How to fix?

    ReplyDelete
  2. I am grateful for this blog to distribute knowledge about this significant topic. Here I found different segments and now I am going to use these new instructions with new enthusiasm.HPE ProLiant ML150 Gen9

    ReplyDelete

Popular Posts