我正在从事 unity3d 项目。我需要从服务器下载一组文件。我使用 C# 编写脚本。经过一个小时的谷歌搜索后,由于文档不完善,我还没有找到解决方案。谁能给我从 url 下载文件并将其保存在 unity3d 中的特定位置的示例代码?
Unity3D 使用称为 Mono 的 C# 实现。单声道支持几乎所有的 http://www.mono-project.com/Compatibility在标准 .NET 库中可用。因此,每当您想知道“我如何在 Unity 中做到这一点?”时,您可以随时查看 .NET 文档,网址为msdn.com http://msdn.microsoft.com/en-us/library/kx37x362.aspx这绝不是贫穷的。关于您的问题,请使用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();
}
}
Edit下载图像文件时,使用DownloadFile
方法提供者WebClient
:
WebClient client = new WebClient();
client.DownloadFile("http://upload.wikimedia.org/wikipedia/commons/5/51/Google.png", @"C:\Images\GoogleLogo.png")
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)