一、根据ping判定网络连接状态
using System;
using System.Collections;
using System.Collections.Generic;
using System.Net.NetworkInformation;
using System.Threading.Tasks;
using UnityEngine;
using Ping = System.Net.NetworkInformation.Ping;
public class test : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
string pingAddr = "realmoon.net";
Func<Task> pingFunc = async () =>
{
await Task.Delay(1);
Ping ping = new Ping();
try
{
var pingReply = ping.Send(pingAddr, 100);
if (pingReply != null && pingReply.Status != IPStatus.Success)
{
print("没有网络");
}
else
{
print("有网络");
}
}
catch (Exception ex)
{
print("没有网络");
Debug.Log(ex.Message);
}
};
Task task = pingFunc();
}
// Update is called once per frame
void Update()
{
}
}
留言