Unity将读取网络路径放到StreamingAssets文件夹下(检测网络是否连接)

该文章展示了两种在Unity中检测网络连接的方法。一种使用协程(Coroutine)进行异步检查,通过UnityWebRequest发送HEAD请求并等待响应。另一种是非协程方式,通过while循环等待UnityWebRequest完成。两种方法均读取配置文件获取URL并处理成功或失败的情况。
摘要由CSDN通过智能技术生成

协程检测网络是否连接

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);
        }
    }
}

非协程检测网络是否连接

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);
        }
    }
 }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值