【问题标题】:Unity check internet connection availabilityUnity 检查互联网连接可用性
【发布时间】:2014-06-22 12:20:03
【问题描述】:

我正在将我们的游戏移植到 Unity,需要一些有关 Unity 中互联网连接检查的帮助。 官方 Unity 文档说“不要使用 Application.internetReachability。 所以我很困惑哪些代码可以在这里工作。 在任何论坛中都没有找到任何突出的解决方案。 我想检查是否打开了 wi-fi 或 GPRS,这应该适用于 iOS 和 Android。 提前致谢。

【问题讨论】:

    标签: ios unity3d reachability


    【解决方案1】:

    解决方案

    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. Unity 的 ping 不做任何域名解析,即它只接受 IP 地址。所以,如果某个玩家可以上网但有一些DNS问题,该方法会说他有互联网。
    2. 这只是一个 ping 检查。不要期望它是 100% 准确的。在极少数情况下,它会为您提供虚假信息。

    【讨论】:

    • 已经检查过了。阅读他们在该链接上的官方说明,上面写着“注意:不要使用此属性来确定实际的连接性。”
    • 这就是我写In conjunction with Ping的原因
    • 对不起,您能提供任何链接或示例代码吗?我试过forum.unity3d.com/threads/…这个链接但没有用。
    • 酷,在编辑器中效果很好!现在将检查 iOS 和 Android。
    • ping.isDone 在 Android 上没有互联网连接的地方是真的!!
    【解决方案2】:

    我做的是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";
        }
    }
    

    【讨论】:

      【解决方案3】:

      这是一个简单的解决方案,我已经在使用它进行 Internet 检查。它适用于IOS和Android。我知道它可能需要改进,但这里是:

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

      【讨论】:

      • 已经提到 Unity 文档不推荐此解决方案。
      猜你喜欢
      • 1970-01-01
      • 2014-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多