2014-06-22 56 views
4

我將我們的遊戲移植到Unity,並需要一些關於Unity中互聯網連接檢查的幫助。 Unity官方文檔說'不要使用Application.internetReachability。 所以我很困惑哪些代碼會在這裏工作。 在任何論壇沒有找到任何突出的解決方案。 我想檢查Wi-Fi或GPRS是否開啓,哪些適用於iOS和Android。 在此先感謝。統一檢查互聯網連接可用性

回答

19

解決方案

Application.internetReachability是你所需要的。可能與Ping一起使用。

下面是一個例子:

using UnityEngine; 

public class InternetChecker : 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 Ping ping; 
    private float pingStartTime; 

    public void Start() 
    { 
     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) 
     { 
      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() 
    { 
     Debug.Log("No Internet :("); 
    } 

    private void InternetAvailable() 
    { 
     Debug.Log("Internet is available! ;)"); 
    } 
} 

需要注意的事項

  1. 統一的平沒有做任何域名的決議,即只接受IP地址。因此,如果某個玩家可以訪問互聯網但存在一些DNS問題,則該方法會說他擁有互聯網。
  2. 這只是一個ping檢查。不要期望它是100%準確的。在極少數情況下,它會爲您提供虛假信息。
+0

已檢查過。閱讀他們關於該鏈接的官方說明,其中說:「注意:不要使用此屬性來確定實際連接。」 – stack

+1

這就是我爲什麼寫'與Ping聯合' –

+0

對不起,請您提供任何鏈接或示例代碼?我嘗試http://forum.unity3d.com/threads/how-can-you-tell-if-there-exists-a-network-connection-of-any-kind.68938/這個鏈接,但沒有奏效。 – stack

1

要真正知道您在線,您需要實施「強制門戶檢測」,以瞭解您是否是例如打到公共WiFi登錄頁面。因此,只需檢查Application.internetReachability或對某個地址執行Ping操作並不能保證您可以成功建立連接或發出WWW請求。

我做了一個簡單的資產稱爲互聯網可達性驗證。無論您是否驗證過互聯網訪問(WWW請求都可以完成),它都會讓您保持最新狀態。 這裏更多的信息:http://j.mp/IRVUN

1

我做的是Network.TestConnection測試對於連接性,它是否能成爲一個服務器,無論是否NAT穿通可以成功地使用。

這是他們給我們的示例代碼。如果結果是ConnectionTesterStatus.Error,則有可能沒有互聯網接入。

var testStatus = "Testing network connection capabilities."; 
var testMessage = "Test in progress"; 
var shouldEnableNatMessage : String = ""; 
var doneTesting = false; 
var probingPublicIP = false; 
var serverPort = 9999; 
var connectionTestResult = ConnectionTesterStatus.Undetermined; 

// Indicates if the useNat parameter be enabled when starting a server 
var useNat = false; 

function OnGUI() { 
    GUILayout.Label("Current Status: " + testStatus); 
    GUILayout.Label("Test result : " + testMessage); 
    GUILayout.Label(shouldEnableNatMessage); 
    if (!doneTesting) 
     TestConnection(); 
} 

function TestConnection() { 
    // Start/Poll the connection test, report the results in a label and 
    // react to the results accordingly 
    connectionTestResult = Network.TestConnection(); 
    switch (connectionTestResult) { 
     case ConnectionTesterStatus.Error: 
      testMessage = "Problem determining NAT capabilities"; 
      doneTesting = true; 
      break; 

     case ConnectionTesterStatus.Undetermined: 
      testMessage = "Undetermined NAT capabilities"; 
      doneTesting = false; 
      break; 

     case ConnectionTesterStatus.PublicIPIsConnectable: 
      testMessage = "Directly connectable public IP address."; 
      useNat = false; 
      doneTesting = true; 
      break; 

     // This case is a bit special as we now need to check if we can 
     // circumvent the blocking by using NAT punchthrough 
     case ConnectionTesterStatus.PublicIPPortBlocked: 
      testMessage = "Non-connectable public IP address (port " + 
       serverPort +" blocked), running a server is impossible."; 
      useNat = false; 
      // If no NAT punchthrough test has been performed on this public 
      // IP, force a test 
      if (!probingPublicIP) { 
       connectionTestResult = Network.TestConnectionNAT(); 
       probingPublicIP = true; 
       testStatus = "Testing if blocked public IP can be circumvented"; 
       timer = Time.time + 10; 
      } 
      // NAT punchthrough test was performed but we still get blocked 
      else if (Time.time > timer) { 
       probingPublicIP = false;  // reset 
       useNat = true; 
       doneTesting = true; 
      } 
      break; 
     case ConnectionTesterStatus.PublicIPNoServerStarted: 
      testMessage = "Public IP address but server not initialized, "+ 
       "it must be started to check server accessibility. Restart "+ 
       "connection test when ready."; 
      break; 

     case ConnectionTesterStatus.LimitedNATPunchthroughPortRestricted: 
      testMessage = "Limited NAT punchthrough capabilities. Cannot "+ 
       "connect to all types of NAT servers. Running a server "+ 
       "is ill advised as not everyone can connect."; 
      useNat = true; 
      doneTesting = true; 
      break; 

     case ConnectionTesterStatus.LimitedNATPunchthroughSymmetric: 
      testMessage = "Limited NAT punchthrough capabilities. Cannot "+ 
       "connect to all types of NAT servers. Running a server "+ 
       "is ill advised as not everyone can connect."; 
      useNat = true; 
      doneTesting = true; 
      break; 

     case ConnectionTesterStatus.NATpunchthroughAddressRestrictedCone: 
     case ConnectionTesterStatus.NATpunchthroughFullCone: 
      testMessage = "NAT punchthrough capable. Can connect to all "+ 
       "servers and receive connections from all clients. Enabling "+ 
       "NAT punchthrough functionality."; 
      useNat = true; 
      doneTesting = true; 
      break; 

     default: 
      testMessage = "Error in test routine, got " + connectionTestResult; 
    } 
    if (doneTesting) { 
     if (useNat) 
      shouldEnableNatMessage = "When starting a server the NAT "+ 
       "punchthrough feature should be enabled (useNat parameter)"; 
     else 
      shouldEnableNatMessage = "NAT punchthrough not needed"; 
     testStatus = "Done testing"; 
    } 
} 
-1

這是一個簡單的解決方案,我已經用於Internet檢查。它適用於IOS和Android。我知道它可能需要改進,但這裏是:

public static bool InternetStatus() 
{ 
    if (Application.internetReachability != NetworkReachability.NotReachable) 
    { 
     return true; 
    }else{ 
     return false; 
    } 
} 
+0

它已經提到有問題的Unity文檔不建議這種解決方案。 –