简体   繁体   English

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

[英]Having Issues Using WebClient to Download Files In Unity

So I am having a big issue when coding for Unity.所以我在为 Unity 编码时遇到了一个大问题。 I am very new to C# and so have used examples I have found online to make this code.我对 C# 非常陌生,因此使用了我在网上找到的示例来制作此代码。 My only issue is there are no errors popping up but it won't download the file properly.我唯一的问题是没有弹出错误,但它不会正确下载文件。

I am using this code so that it will be easy for my users to import unitypackages they would use often.我正在使用此代码,以便我的用户可以轻松导入他们经常使用的统一包。 I have a button that works as intended, it shows download and then changes to import if the file exists.我有一个按预期工作的按钮,它显示下载,然后如果文件存在则更改为导入。 However, if I click it when it says download it will instantly say "Download Complete" and the file won't show up for a few minutes.但是,如果我在它说下载时单击它,它会立即说“下载完成”,并且该文件将在几分钟内不显示。 When it finally does the file is 0KB in size.当它最终完成时,文件的大小为 0KB。

I really need help figured out why my file isn't downloading properly.我真的需要帮助弄清楚为什么我的文件没有正确下载。 I am super stumped.我超级难过。

This code is the script for the WebClient.此代码是 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);
    }
}

} }

This code is the button calling the download from my other script.此代码是从我的其他脚本调用下载的按钮。

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

So I figured out my issue, but I am not sure why it was an issue.所以我想出了我的问题,但我不知道为什么这是一个问题。 Apparently putting "https://" for the urlstart instead of "http://" broke it, despite you being able to download the file normally regardless of the protocol used.显然,为 urlstart 放置“https://”而不是“http://”会破坏它,尽管无论使用何种协议,您都可以正常下载文件。 If someone can help me figure out why that is I would be grateful!如果有人可以帮助我弄清楚为什么会这样,我将不胜感激!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM