Unity解决:没有UnityWebRequest.Result

81 篇文章 1 订阅

当我在Unity 2019中使用Unity 2021的代码satable时。
控制台显示
“UnityWebRequest”不包含“result”的定义,并且找不到接受“UnityWebRequest”类型的第一个参数的可访问扩展方法“result”(是否缺少using指令或程序集引用?)
漏洞/问题:

if (req.result == UnityWebRequest.Result.ConnectionError || req.result == UnityWebRequest.Result.ProtocolError)

版本2020.3中添加了result
在该版本之前,只需遵循相应版本API中的示例,例如2019.4 API
例如,您可以简单地检查error中是否有任何内容

using (var webRequest = UnityWebRequest.Get(uri))
{
    yield return webRequest.SendWebRequest();
    if (!string.IsNullOrWhiteSpace(webRequest.error))
    {
        Debug.LogError($"Error {webRequest.responseCode} - {webRequest.error}");
        yield break;
    }
    Debug.Log(webRequest.downloadHandler.text);
}

或者如果您想进一步区分isNetworkError(包括没有互联网连接、主机不可访问、DNS解析错误等错误)和isHttpError(基本上与responseCode >= 400相同)
如果你的问题是关于向下兼容性,但支持两个版本要么坚持2020.3之前的方式或使用Conditional Compilation和做例如.

#if UNITY_2020_3_OR_NEWER
    if(webRequest.result == UnityWebRequest.Result.ConnectionError || webRequest.result == UnityWebRequest.Result.ProtocolError)  
#else
    if(!string.IsNullOrWhiteSpace(webRequest.error))
#endif
    {
        Debug.LogError($"Error {webRequest.responseCode} - {webRequest.error}");
        yield break;
    }

或者如下:

   /// <summary>
    /// 执行下载行为
    /// </summary>
    /// <param name="bundleInfoList"></param>
    /// <returns>返回的List包含的是还未下载的Bundle</returns>
    private async Task<List<BundleInfo>> ExecuteDownload(ModuleConfig moduleConfig, List<BundleInfo> bundleList)
    {
        while (bundleList.Count > 0)
        {
            BundleInfo bundleInfo = bundleList[0];

            UnityWebRequest request = UnityWebRequest.Get(GetServerURL(moduleConfig, bundleInfo.bundle_name));

            string updatePath = GetUpdatePath(moduleConfig.moduleName);

            request.downloadHandler = new DownloadHandlerFile(string.Format("{0}/" + bundleInfo.bundle_name, updatePath));

            await request.SendWebRequest();
            
#if UNITY_2020_3_OR_NEWER

            if (request.result == UnityWebRequest.Result.Success)
            {
                Debug.Log("下载资源:" + bundleInfo.bundle_name + " 成功");

                bundleList.RemoveAt(0);
            }
            else
            {
                break;
            }

#else
            if (string.IsNullOrEmpty(request.error) == true)
            {
                Debug.Log("下载资源:" + bundleInfo.bundle_name + " 成功");

                bundleList.RemoveAt(0);
            }
            else
            {
                break;
            }
#endif
            

            
        }

        return bundleList;
    }

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
你可以使用UnityWebRequest来实现这个功能。UnityWebRequest支持断点续传、MD5校验和进度条显示,并且可以通过检查webRequest.result属性来判断请求是否成功。 以下是一个示例代码,演示如何下载文件并显示下载进度: ```csharp using System.Collections; using UnityEngine; using UnityEngine.Networking; public class FileDownloader : MonoBehaviour { public string fileUrl; public string savePath; private UnityWebRequest webRequest; public IEnumerator DownloadFile() { webRequest = UnityWebRequest.Get(fileUrl); webRequest.downloadHandler = new DownloadHandlerFile(savePath); // 获取已下载的字节数 long startPos = GetExistingFileSize(savePath); if (startPos > 0) { // 设置断点续传的起始位置 webRequest.SetRequestHeader("Range", "bytes=" + startPos + "-"); } webRequest.SendWebRequest(); while (!webRequest.isDone) { float progress = webRequest.downloadProgress; Debug.Log("Download progress: " + progress); yield return null; } if (webRequest.result == UnityWebRequest.Result.ConnectionError || webRequest.result == UnityWebRequest.Result.DataProcessingError || webRequest.result == UnityWebRequest.Result.ProtocolError) { Debug.LogError("Download failed: " + webRequest.error); } else { Debug.Log("Download complete!"); // 验证文件MD5 string md5 = ComputeMD5(savePath); if (md5 != GetFileMD5(fileUrl)) { Debug.LogError("MD5 check failed!"); } } } // 获取已下载的字节数 private long GetExistingFileSize(string filePath) { if (File.Exists(filePath)) { FileInfo fileInfo = new FileInfo(filePath); return fileInfo.Length; } return 0; } // 计算文件的MD5值 private string ComputeMD5(string filePath) { using (var md5 = System.Security.Cryptography.MD5.Create()) { using (var stream = File.OpenRead(filePath)) { byte[] hash = md5.ComputeHash(stream); return BitConverter.ToString(hash).Replace("-", "").ToLower(); } } } // 获取文件的MD5值 private string GetFileMD5(string fileUrl) { using (var webClient = new WebClient()) { byte[] data = webClient.DownloadData(fileUrl); using (var md5 = System.Security.Cryptography.MD5.Create()) { byte[] hash = md5.ComputeHash(data); return BitConverter.ToString(hash).Replace("-", "").ToLower(); } } } } ``` 在上面的示例中,我们通过UnityWebRequest.Get方法创建一个下载请求,设置下载进度和下载完成后保存文件的路径。我们使用SetRequestHeader方法设置断点续传的起始位置,使用ComputeMD5方法计算下载完成后文件的MD5值,并使用GetFileMD5方法获取文件在服务器上的MD5值,最后对比两个MD5值是否相同。 注意:在下载文件之前,需要确保设置了网络权限。在Android平台上,需要在AndroidManifest.xml文件中添加以下权限: ```xml <uses-permission android:name="android.permission.INTERNET" /> ``` 这样就可以通过UnityWebRequest来下载文件,并支持断点续传、MD5校验和进度条显示了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AD_喵了个咪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值