问题描述
iam从事unity3d项目.我需要从服务器下载文件集.我使用C#编写脚本.谷歌一个小时后,由于文档不佳,我没有找到解决方案.谁能给我示例代码,从URL下载文件并将其保存在unity3d中的特定位置?
iam working on unity3d project. i need set of files to be downloaded from the server. iam using C# for scripting. after a hour of google i haven't found a solution because of poor documentation. can anyone give me example code for download file from url and save it in a specific location in unity3d?
推荐答案
Unity3D使用称为Mono的C#实现. Mono支持标准.NET库中可用的几乎所有内容.因此,每当您想知道如何在Unity中做到这一点?"时,始终可以查看位于 msdn.com 绝不差.关于您的问题,请使用WebClient
类:
Unity3D uses the implementation of C# known as Mono. Mono supports almost everything available in the standard .NET library. Thus, whenever you are wondering 'How do I do that in Unity?', you can always take a look at the documentation for .NET available at msdn.com which is by no way poor. Regarding your question, use the WebClient
class:
using System;
using System.Net;
using System.IO;
public class Test
{
public static void Main (string[] args)
{
WebClient client = new WebClient();
Stream data = client.OpenRead(@"http://google.com");
StreamReader reader = new StreamReader(data);
string s = reader.ReadToEnd();
Console.WriteLine(s);
data.Close();
reader.Close();
}
}
修改下载图像文件时,请使用WebClient
提供的DownloadFile
方法:
EditWhen downloading an image file, use the DownloadFile
method provided by WebClient
:
WebClient client = new WebClient();
client.DownloadFile("http://upload.wikimedia.org/wikipedia/commons/5/51/Google.png", @"C:\Images\GoogleLogo.png")
这篇关于如何从URL下载文件并在C Sharp中使用unity3d保存在位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!