2016-10-17 2 views
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);을 사용하고 단일 프레임 지연의 경우 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);으로 바꿔서 핑이 아직 완료되었는지 확인하기 위해 500 밀리 초를 기다릴 수도 있습니다. 30fps로 설정하면 15 프레임마다 핑이 수행되는지 확인하기 위해 약간의 오버 헤드 비용 만 발생합니다.