C# 在Unity中使用WebClient下载文件时出现问题
所以我在为Unity编写代码时遇到了一个大问题。我对C#非常陌生,因此使用了我在网上找到的示例来编写此代码。我唯一的问题是没有出现错误,但它无法正确下载文件 我使用这段代码是为了方便我的用户导入他们经常使用的unitypackages。我有一个按钮,可以按预期工作,它显示下载,然后更改为导入(如果文件存在)。但是,如果我在它显示下载时单击它,它会立即显示“下载完成”,并且文件在几分钟内不会显示。当它最终执行此操作时,文件大小为0KB 我真的需要帮助找出我的文件下载不正确的原因。我被难住了 此代码是WebClient的脚本。C# 在Unity中使用WebClient下载文件时出现问题,c#,unity3d,download,webclient,C#,Unity3d,Download,Webclient,所以我在为Unity编写代码时遇到了一个大问题。我对C#非常陌生,因此使用了我在网上找到的示例来编写此代码。我唯一的问题是没有出现错误,但它无法正确下载文件 我使用这段代码是为了方便我的用户导入他们经常使用的unitypackages。我有一个按钮,可以按预期工作,它显示下载,然后更改为导入(如果文件存在)。但是,如果我在它显示下载时单击它,它会立即显示“下载完成”,并且文件在几分钟内不会显示。当它最终执行此操作时,文件大小为0KB 我真的需要帮助找出我的文件下载不正确的原因。我被难住了 此代码
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();
}
此代码是从我的其他脚本调用下载的按钮。
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();
所以我找到了我的问题,但我不确定为什么会有问题。显然,将“https://”改为“http://”会破坏urlstart,尽管无论使用何种协议,您都可以正常下载该文件。如果有人能帮我找出原因,我将不胜感激 您的
localDownloadPath
是否存在?如果没有,则WebClient
将立即返回finished。尝试设置一个绝对路径(只是为了验证)。我解决了这个问题,但localDownloadPath确实存在。非常感谢。