2016-10-17 16 views
2

私は決定を下すためにインターネットが利用可能かどうかを確認する必要があります。インターネットが作動しているときとインターネットが作動していないときの2つの異なるタイプの決定があります。私は方法を見つけましたが、それはCPUのためのオーバーヘッドを作成します。現在、このタイプのソリューションを使用しています。インターネットが利用可能かどうかを確認する良い方法はありますか?確認方法Unity3dでオーバーヘッドを発生させずにインターネットを利用可能

using UnityEngine; 
using UnityEngine.UI; 
using System.Collections; 

public class DeviceConnected : MonoBehaviour 
{ 
    private const bool allowCarrierDataNetwork = false; 
    private const string pingAddress = "8.8.8.8"; // Google Public DNS server 
    private const float waitingTime = 2.0f; 
    public Text txtInternetConnectStatus; 
    private Ping ping; 
    private float pingStartTime; 
    private bool isInternetAvailable = false; 

    public void Start() 
    { 
     InvokeRepeating("OnStartCheck", 0f,3.0f); 

    } 

    public void OnStartCheck() 
    { 
     bool internetPossiblyAvailable; 
     switch (Application.internetReachability) 
     { 
      case NetworkReachability.ReachableViaLocalAreaNetwork: 
       internetPossiblyAvailable = true; 
       break; 
      case NetworkReachability.ReachableViaCarrierDataNetwork: 
       internetPossiblyAvailable = allowCarrierDataNetwork; 
       break; 
      default: 
       internetPossiblyAvailable = false; 
       break; 
     } 
     if (!internetPossiblyAvailable) 
     { 
      InternetIsNotAvailable(); 
      return; 
     } 
     ping = new Ping(pingAddress); 
     pingStartTime = Time.time; 
    } 
    public void Update() 
    { 
     if (ping != null) 
     { 
      Debug.Log("Hi"); 
      bool stopCheck = true; 
      if (ping.isDone) 
      { 
       if (ping.time >= 0) 
        InternetAvailable(); 
       else 
        InternetIsNotAvailable(); 
      } 
      else if (Time.time - pingStartTime < waitingTime) 
       stopCheck = false; 
      else 
       InternetIsNotAvailable(); 
      if (stopCheck) 
       ping = null; 
     } 
    } 



    private void InternetIsNotAvailable() 
    { 
     if (isInternetAvailable == false) 
     { 
      Debug.Log("No Internet :("); 
      txtInternetConnectStatus.text = "No Internet :("; 
      isInternetAvailable = true; 
     } 
    } 

    private void InternetAvailable() 
    { 
     if (isInternetAvailable==true) 
     { 
      Debug.Log("Internet is available! ;)"); 
      txtInternetConnectStatus.text = "Internet is available! ;)"; 
      isInternetAvailable = false; 

     } 

    } 


    public void OnClickShowMediationSuite() 
    { 
     ShowAds.instance.ShowMediationSuite(); 
    } 
} 
+1

オーバーヘッドなし?オーバーヘッドを起こさない唯一のことは、何もしないことだけです。あなたはこのことをより明確に説明しなければなりません。 –

答えて

0

これは、ユニティなしでオンザフライで書かれているため、多少の誤差が生じる可能性があります。このコードではInvokeRepeatingの代わりにStartCorutineが使用され、繰り返し部分は長い遅延の場合はyield return WaitForSeconds(timeBetweenChecks);、1つのフレーム遅延の場合はyield return null;を使用して関数内のループになりました。

public class DeviceConnected : MonoBehaviour 
{ 
    private const bool allowCarrierDataNetwork = false; 
    private const string pingAddress = "8.8.8.8"; // Google Public DNS server 
    private const float waitingTime = 2.0f; 
    private const float timeBetweenChecks = 3.0f; 
    public Text txtInternetConnectStatus; 
    private float pingStartTime; 
    private bool isInternetAvailable = false; 

    public void Start() 
    { 
     //Start out with the assumption that the internet is not available. 
     InternetIsNotAvailable(); 

     StartCoroutine(InternetCheck()); 

    } 

    public IEnumerator InternetCheck() 
    { 
     //If you want it to stop checking for internet once it has a successful connection, 
     //just remove "while (true)" loop 
     while (true) 
     { 
      bool internetPossiblyAvailable = false; 
      while (!internetPossiblyAvailable) 
      { 
       switch (Application.internetReachability) 
       { 
        case NetworkReachability.ReachableViaLocalAreaNetwork: 
         internetPossiblyAvailable = true; 
         break; 
        case NetworkReachability.ReachableViaCarrierDataNetwork: 
         internetPossiblyAvailable = allowCarrierDataNetwork; 
         break; 
        default: 
         internetPossiblyAvailable = false; 
         break; 
       } 
       if (!internetPossiblyAvailable) 
       { 
        InternetIsNotAvailable(); 
        //Wait to check again. 
        yield return WaitForSeconds(timeBetweenChecks); 
       } 
      } 

      Ping ping = new Ping(pingAddress); 
      pingStartTime = Time.time; 

      Debug.Log("Hi"); 
      bool stopCheck = true; 

      while (!ping.isDone && Time.time - pingStartTime < waitingTime) 
      { 
       //Wait one frame; 
       yield return null; 
      } 

      if (ping.isDone && ping.time >= 0) 
       InternetAvailable(); 
      else 
       InternetIsNotAvailable(); 

      //Wait to check again. 
      yield return WaitForSeconds(timeBetweenChecks); 

     } 
    } 
    private void InternetIsNotAvailable() 
    { 
     //Only log when we are going from true to flase. 
     if (isInternetAvailable != false) 
     { 
      Debug.Log("No Internet :("); 
      txtInternetConnectStatus.text = "No Internet :("; 
      isInternetAvailable = false; //This was changed from true to false. 
     } 
    } 

    private void InternetAvailable() 
    { 
     //Only log when we are going from false to true. 
     if (isInternetAvailable != true) 
     { 
      Debug.Log("Internet is available! ;)"); 
      txtInternetConnectStatus.text = "Internet is available! ;)"; 
      isInternetAvailable = true; //This was changed from false to true 
     } 

    } 

    public void OnClickShowMediationSuite() 
    { 
     ShowAds.instance.ShowMediationSuite(); 
    } 
} 

これは、間違いなくオーバーヘッドコストが低くなります。 yield return nullyield return WaitForSeconds(0.5f);に置き換えることもできます。そのため、pingがまだ完了していないかどうかを確認するために500ミリ秒間待機します。 30fpsでは、pingが15フレームごとに1回実行されるかどうかを確認するためのわずかなオーバーヘッドコストしかかかりません。

関連する問題