使用WebClient在Unity中下载文件时遇到问题

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

我正在使用此代码,以便我的用户可以轻松导入他们经常使用的unitypackage。我有一个按预期工作的按钮,它显示下载,如果文件存在,则更改为导入。但是,如果单击下载时单击它,则会立即显示“ Download Complete”,并且文件将在几分钟内不显示。最终,文件大小为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();
bin_swnswn 回答:使用WebClient在Unity中下载文件时遇到问题

所以我想出了我的问题,但是我不确定为什么是一个问题。尽管无论使用何种协议都可以正常下载文件,但显然在URLstart上使用“ https://”而不是“ http://”会破坏它。如果有人可以帮助我找出原因,我将不胜感激!

本文链接:https://www.f2er.com/3127299.html

大家都在问