Coroutine detecta si la red está conectada
using UnityEngine;
public class URLChecker : MonoBehaviour
{
public string configFileName = "config.txt";
using UnityEngine;
using UnityEngine.Networking;
public class URLChecker : MonoBehaviour
{
public string configFileName = "config.txt";
void Start()
{
// 构建配置文件的完整路径
string filePath = System.IO.Path.Combine(Application.streamingAssetsPath, configFileName);
// 读取配置文件获取网址
string urlToCheck = ReadConfigFile(filePath);
// 进行URL检查
StartCoroutine(CheckURL(urlToCheck));
}
string ReadConfigFile(string filePath)
{
string url = "";
if (System.IO.File.Exists(filePath))
{
url = System.IO.File.ReadAllText(filePath).Trim();
}
else
{
Debug.LogError("Config file not found: " + filePath);
}
return url;
}
IEnumerator CheckURL(string url)
{
UnityWebRequest www = UnityWebRequest.Head(url);
yield return www.SendWebRequest();
if (www.result == UnityWebRequest.Result.Success)
{
Debug.Log("URL is accessible.");
}
else
{
Debug.Log("URL is not accessible. Error: " + www.error);
}
}
}
La no rutina detecta si la red está conectada
using UnityEngine;
using UnityEngine.Networking;
public class URLChecker : MonoBehaviour
{
public string configFileName = "config.txt";
public bool isWeb;
void Start()
{
// 构建配置文件的完整路径
string filePath = System.IO.Path.Combine(Application.streamingAssetsPath, configFileName);
// 读取配置文件获取网址
string urlToCheck = ReadConfigFile(filePath);
// 进行URL检查
CheckURL(urlToCheck);
}
string ReadConfigFile(string filePath)
{
string url = "";
if (System.IO.File.Exists(filePath))
{
url = System.IO.File.ReadAllText(filePath).Trim();
}
else
{
Debug.LogError("Config file not found: " + filePath);
}
return url;
}
void CheckURL(string url)
{
UnityWebRequest www = UnityWebRequest.Head(url);
www.SendWebRequest();
while (!www.isDone)
{
// 等待网络请求完成
}
if (www.result == UnityWebRequest.Result.Success)
{
IsWeb = true;
Debug.Log("已连接网络..............................................");
}
else
{
IsWeb = false;
Debug.Log("无法连接网络.................................................: " + www.error);
}
}
}