【问题标题】:How to Check is Internet Available without creating overhead in Unity3d如何在 Unity3d 中检查 Internet 是否可用而不产生开销
【发布时间】:2017-02-26 13:48:56
【问题描述】:

我需要检查我的游戏中是否可以使用互联网,以便我做出决定。当互联网工作和互联网不工作时,有两种不同类型的决定。我找到了一种方法,但它会为 CPU 带来开销。目前我正在使用这种类型的解决方案。有没有更好的方法来检查互联网是否可用?

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();
    }
}

【问题讨论】:

  • 没有开销?我很确定唯一不会产生开销的事情就是什么都不做。你必须更清楚地解释你的意思。

标签: user-interface unity3d internet-connection


【解决方案1】:

这是在没有 Unity 的情况下即时编写的,因此可能会出现一些错误。此代码现在使用StartCorutine 而不是InvokeRepeating,重复部分现在是函数内部的循环,使用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 null 替换为yield return WaitForSeconds(0.5f);,这样它会等待500 毫秒来检查ping 是否完成。在 30 fps 下,这意味着您只需很少的开销来检查是否每 15 帧执行一次 ping。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-15
    • 1970-01-01
    • 2013-01-28
    • 2020-04-26
    • 2013-08-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多