簡體   English   中英

在 Unity 中使用 WebClient 下載文件時遇到問題

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

所以我在為 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();

所以我想出了我的問題,但我不知道為什么這是一個問題。 顯然,為 urlstart 放置“https://”而不是“http://”會破壞它,盡管無論使用何種協議,您都可以正常下載文件。 如果有人可以幫助我弄清楚為什么會這樣,我將不勝感激!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM