如何从网址下载文件并使用C sharp中的unity3d保存在位置?

sky*_*sky 2 c# unity-game-engine

我正致力于unity3d项目.我需要从服务器下载一组文件.我使用C#编写脚本.经过一个小时的谷歌我没有找到解决方案,因为文档很差.任何人都可以从url给我下载文件的示例代码并将其保存在unity3d中的特定位置?

Nik*_*off 6

Unity3D使用称为Mono的C#实现.Mono支持标准.NET库中几乎所有可用的东西.因此,无论何时你想知道"我如何在Unity中做到这一点?",你总是可以看看msdn.com上提供的.NET文档,这绝不是一件好事.关于你的问题,请使用WebClient课程:

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑 下载图像文件时,请使用以下DownloadFile方法提供的方法WebClient:

 WebClient client = new WebClient();
 client.DownloadFile("http://upload.wikimedia.org/wikipedia/commons/5/51/Google.png", @"C:\Images\GoogleLogo.png")
Run Code Online (Sandbox Code Playgroud)