如何检查Internet是否可用而不会在Unity3d中创建开销

时间:2016-10-17 14:13:24

标签: user-interface unity3d internet-connection

我需要检查我的游戏中是否可以使用互联网,以便我可以做出决定。当互联网工作和互联网不工作时,有两种不同类型的决定。我找到了一种方法,但它为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();
    }
}

1 个答案:

答案 0 :(得分:0)

这是在没有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操作。