Unity3D:技巧(完成):判断设备是否有外网
目录
一.目的
1.想知道:Unity判断设备是否有外网
二.参考
三.注意
四.操作:成功
1.代码
/// <summary>
/// [Author:xzy][Time:20210906]
/// [Function:Check whether the Internet is connected; 功能:判断是否连接外网]
/// </summary>
/// <returns></returns>
private bool GetIsCanPingNetAddress()
{
bool bIsCanPingNetAddress = false;
//当网络不可用时
if (Application.internetReachability == NetworkReachability.NotReachable)
Debug.Log("无网络—环境");
//当用户使用WiFi时
if (Application.internetReachability == NetworkReachability.ReachableViaLocalAreaNetwork)
Debug.Log("wifi/网线—环境");
//当用户使用移动网络时
if (Application.internetReachability == NetworkReachability.ReachableViaCarrierDataNetwork)
Debug.Log("运营商数据网络—环境");
try
{
System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
PingReply pr = ping.Send("www.baidu.com", 1000);
if (pr.Status == IPStatus.Success)
{
Debug.Log("网络连接信号正常");
bIsCanPingNetAddress=true;
}
else
{
Debug.Log("网络连接无信号");
bIsCanPingNetAddress = false;
}
}
catch (Exception e)
{
Debug.Log("网络连接信号异常" + e.Message);
bIsCanPingNetAddress = false;
}
return bIsCanPingNetAddress;
}