Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
User interface 如何在Unity3d中检查Internet是否可用而不产生开销_User Interface_Unity3d_Internet Connection - Fatal编程技术网

User interface 如何在Unity3d中检查Internet是否可用而不产生开销

User interface 如何在Unity3d中检查Internet是否可用而不产生开销,user-interface,unity3d,internet-connection,User Interface,Unity3d,Internet Connection,我需要检查我的游戏中是否有互联网,以便我做出决定。有两种不同类型的决定,当互联网工作时和当互联网不工作时。我找到了一种方法,但它会给cpu带来开销。目前我正在使用这种类型的解决方案。有没有更好的方法来检查互联网是否可用 using UnityEngine; using UnityEngine.UI; using System.Collections; public class DeviceConnected : MonoBehaviour { private const bool al

我需要检查我的游戏中是否有互联网,以便我做出决定。有两种不同类型的决定,当互联网工作时和当互联网不工作时。我找到了一种方法,但它会给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();
    }
}
使用UnityEngine;
使用UnityEngine.UI;
使用系统集合;
公共类设备连接:单行为
{
private const bool allowCarrierDataNetwork=false;
private const string pingAddress=“8.8.8.8”;//谷歌公共DNS服务器
private const float waitingTime=2.0f;
公共文本txtInternetConnectStatus;
私人平平,;
私人浮动时间;
私有布尔值isInternetAvailable=false;
公开作废开始()
{
调用重复(“OnStartCheck”,0f,3.0f);
}
公共无效OnStartCheck()
{
bool互联网可能可用;
交换机(应用程序可互连性)
{
案例网络可访问性。可通过LocalAreaNetwork访问:
internetPossiblyAvailable=真;
打破
案例网络可访问性。可访问载体数据网络:
internetPossiblyAvailable=allowCarrierDataNetwork;
打破
违约:
internetPossiblyAvailable=错误;
打破
}
如果(!internetPossiblyAvailable)
{
互联网不可用();
返回;
}
ping=新ping(ping地址);
pingStartTime=Time.Time;
}
公共无效更新()
{
如果(ping!=null)
{
Debug.Log(“Hi”);
bool stopCheck=true;
如果(ping.isDone)
{
如果(ping.time>=0)
互联网可用();
其他的
互联网不可用();
}
else if(Time.Time-pingStartTime
这是在没有统一的情况下动态编写的,因此可能会出现一些错误。此代码现在使用
startcrutine
而不是
invokererepeating
,重复部分现在是函数内部的循环,使用
产生返回等待秒(timeBetweenChecks)产生返回空值用于单帧延迟

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();
    }
}
公共类设备连接:单行为
{
private const bool allowCarrierDataNetwork=false;
private const string pingAddress=“8.8.8.8”;//谷歌公共DNS服务器
private const float waitingTime=2.0f;
检查之间的专用常量浮动时间=3.0f;
公共文本txtInternetConnectStatus;
私人浮动时间;
私有布尔值isInternetAvailable=false;
公开作废开始()
{
//从互联网不可用的假设开始。
互联网不可用();
开始例行程序(InternetCheck());
}
公共IEnumerator InternetCheck()
{
//如果您希望它在成功连接后停止检查internet,
//只需删除“while(true)”循环即可
while(true)
{
bool internetPossiblyAvailable=错误;
而(!internetPossiblyAvailable)
{
交换机(应用程序可互连性)
{
案例网络可访问性。可通过LocalAreaNetwork访问:
internetPossiblyAvailable=真;
打破
案例网络可访问性。可访问载体数据网络:
internetPossiblyAvailable=allowCarrierDataNetwork;
打破
违约:
internetPossiblyAvailable=错误;
打破
}
如果(!internetPossiblyAvailable)
{
互联网不可用();
//等待再次检查。
收益返回等待秒(检查之间的时间);
}
}
Ping Ping=新Ping(Ping地址);
pingStartTime=Time.Time;
Debug.Log(“Hi”);
bool stopCheck=true;
而(!ping.isDone&&Time.Time-pingStartTime=0)
互联网可用();
其他的
互联网不可用();
//等待再次检查。
收益返回等待秒(检查之间的时间);
}
}
私有无效互联网不可用()
{
//只有当我们从真变假时才记录。
如果(isInternetAvailable!=false)
{
Debug.Log(“没有互联网:(”);
txtInternetConnectStatus.text=“没有互联网:(”;
isInternetAvailable=false;//此值已从true更改为false。
}
}
私有无效互联网可用()
{
//在