【问题标题】:Having Issues Using WebClient to Download Files In Unity在 Unity 中使用 WebClient 下载文件时遇到问题
【发布时间】:2020-03-06 16:22:38
【问题描述】:

所以我在为 Unity 编码时遇到了一个大问题。我对 C# 非常陌生,因此使用了我在网上找到的示例来编写此代码。我唯一的问题是没有弹出错误,但它不会正确下载文件。

我正在使用此代码,以便我的用户可以轻松导入他们经常使用的统一包。我有一个按预期工作的按钮,它显示下载,然后如果文件存在则更改为导入。但是,如果我在它说下载时单击它,它会立即说“下载完成”,并且文件不会出现几分钟。当它最终完成时,文件的大小为 0KB。

我真的需要帮助找出我的文件没有正确下载的原因。我超级难过。

此代码是 WebClient 的脚本。

using UnityEngine;
using System.IO;
using System.Net;
using System;
using System.ComponentModel;
using UnityEditor;      

namespace SentinelsSDK
{
public class SentinelsSDK_ImportManager
{
    private static string localPath = "Assets/VRCSDK/Dependencies/VRChat/Imports/";
    private static string localDownloadPath = "Assets/VRCSDK/Dependencies/VRChat/Imports/";
    private static string urlStart = "https://www.sentinels.xyz/uploads/2/0/9/0/20909832/";

    public static void DownloadAndImportAssetFromServer(string assetName)
    {
        if (File.Exists(localDownloadPath + assetName))
        {
            sentLog(assetName + " exists. Importing it..");
            importDownloadedAsset(assetName);
        }
        else
        {
            sentLog(assetName + " does not exist. Starting download..");
            downloadFile(assetName);
        }
    }

    private static void downloadFile(string assetName)
    {
        WebClient w = new WebClient();
        w.Headers.Set(HttpRequestHeader.UserAgent, "Webkit Gecko wHTTPS (Keep Alive 55)");
        w.QueryString.Add("assetName", assetName);
        w.DownloadFileCompleted += fileDownloadCompleted;
        w.DownloadProgressChanged += fileDownloadProgress;
        string url = urlStart + assetName;
        w.DownloadFileAsync(new Uri(url), localDownloadPath + assetName);
    }

    private static void fileDownloadCompleted(object sender, AsyncCompletedEventArgs e)
    {
        string assetName = ((WebClient)(sender)).QueryString["assetName"];
        sentLog("Download of file " + assetName + " completed!");
    }

    private static void fileDownloadProgress(object sender, DownloadProgressChangedEventArgs e)
    {
        sentLog("Progress is at " + e.ProgressPercentage.ToString() + "%");
    }

    private static void sentLog(string message)
    {
        Debug.Log("[SentinelsSDK] AssetDownloader: " + message);
    }

    public static void importAsset(string assetName)
    {
        AssetDatabase.ImportPackage(localPath + assetName, true);
    }

    public static void importDownloadedAsset(string assetName)
    {
        AssetDatabase.ImportPackage(localDownloadPath + assetName, true);
    }
}

}

此代码是从我的其他脚本调用下载的按钮。

using SentinelsSDK;
    ...
    private static string localDownloadPath = "Assets/VRCSDK/Dependencies/VRChat/Imports/";
    ...
    GUILayout.BeginHorizontal();
    if (GUILayout.Button((File.Exists(localDownloadPath + "poiyomitoon.unitypackage") ? "Import" : "Download") + " - Poiyomi Toon"))
      {
          SentinelsSDK_ImportManager.DownloadAndImportAssetFromServer("poiyomitoon.unitypackage");
      }
    GUILayout.EndHorizontal();

【问题讨论】:

  • 你的localDownloadPath 存在吗?如果没有,WebClient 将立即返回完成。尝试设置绝对路径(仅用于验证)。
  • 我发现了问题,但 localDownloadPath 确实存在。谢谢

标签: c# unity3d download webclient


【解决方案1】:

所以我发现了我的问题,但我不确定为什么会出现问题。显然,为 urlstart 放置“https://”而不是“http://”会破坏它,尽管无论使用何种协议,您都可以正常下载文件。如果有人能帮我弄清楚为什么会这样,我将不胜感激!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-22
    • 2011-12-03
    • 1970-01-01
    • 2019-09-13
    • 1970-01-01
    • 1970-01-01
    • 2018-12-25
    • 1970-01-01
    相关资源
    最近更新 更多