【问题标题】:WebClient Class in build for Windows Store - Build Errors in UnityWindows 应用商店构建中的 WebClient 类 - Unity 中的构建错误
【发布时间】:2017-02-24 22:15:49
【问题描述】:

我正在为 Windows Store 平台(Universal 10 SDK)构建一个统一的应用程序(5.4.0f3)。 我将在 hololens 上运行它。

我在编译脚本时遇到问题,这似乎是由于我正在使用的 WebClient 类。我也尝试按照另一篇文章中的建议使用 HttpClient 但没有运气。我看到有些人在 Unity 中使用 WebClient 类成功构建,但我猜他们不是为 Windows 应用商店构建的。

我得到的编译错误: 错误 CS0246:找不到类型或命名空间名称“WebClient”(您是否缺少 using 指令或程序集引用?) 当前上下文中不存在名称“webClient”

我刚刚开始使用 Unity,但我相信我可以在使用 WebClient 或声明新 WebClient 的代码周围添加一些指令,以便它仍然可以编译并能够在 hololens 上运行。

我发现“平台相关编译”页面 (https://docs.unity3d.com/Manual/PlatformDependentCompilation.html) 似乎可以解释这一点。 我尝试使用其中的一些(例如 UNITY_WSA、UNITY_WSA_10_0 等),但没有运气。我目前以下列方式使用 yahoo Finance API:webClient.DownloadFile(url, stockFile); 下载 .csv 文件。 有什么建议吗?

【问题讨论】:

    标签: c# unity3d windows-store-apps win-universal-app hololens


    【解决方案1】:

    我注意到您使用了WebClient.DownloadFile。您应该使用DownloadFile 函数,因为这将等待文件完成下载。这次发生的情况是,由于主线程被阻塞,所以 UI 将被冻结。

    如果要使用WebClient API,则应使用DownloadFileAsync 函数。 Here 就是一个例子。

    您可以在 Hololens 以外的平台上使用 WebClient。然后您可以在全息镜头上使用WWWUnityWebRequest,然后在下载完成后使用File.WriteAllBytes 保存文件。

    伪代码应如下所示:

    #if !UNITY_WSA_10_0
    WebClient API
    #else
    WWW API
    #endif
    

    完整代码:

    string url = "http://www.yourUrl.com";
    string savePath = Path.Combine(Application.dataPath, "file.txt");
    int downloadID = 0;
    
    #if !UNITY_WSA_10_0
    //Will be used on Platforms other than Hololens
    void downloadFile()
    {
        WebClient webClient = new WebClient();
        webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(DoSomethingOnFinish);
        webClient.QueryString.Add("fileName", downloadID.ToString());
        Uri uri = new Uri(url);
        webClient.DownloadFileAsync(uri, savePath);
        downloadID++;
    }
    
    void DoSomethingOnFinish(object sender, AsyncCompletedEventArgs e)
    {
        string myFileNameID = ((System.Net.WebClient)(sender)).QueryString["fileName"];
    }
    
    #else
    //Will be used on Hololens
    void downloadFile()
    {
        StartCoroutine(downloadFileCOR());
    }
    
    IEnumerator downloadFileCOR()
    {
        WWW www = new WWW(url);
        yield return www;
    
        Debug.Log("Finished Downloading file");
    
        byte[] yourBytes = www.bytes;
    
        //Now Save it
        System.IO.File.WriteAllBytes(savePath, yourBytes);
    }
    #endif
    

    用法

    downloadFile();
    

    如果您仍然收到错误,那么您也在使用 Hololens 不支持的命名空间。你也应该处理它。假设命名空间是System.Net;,你可以像下面这样修复它:

    #if !UNITY_WSA_10_0
    using System.Net;
    #endif
    

    【讨论】:

    • 太棒了!如何判断协程是否返回?
    • 使用Action 来做到这一点。看看this。如果您仍然感到困惑,请为此提出新问题。
    【解决方案2】:

    正如程序员在他的回答中提到的那样,HoloLens 平台上不存在 webclient,除了滚动你自己的,因为他建议另一种选择是使用 Unity Asset Store 中可用的客户端之一。我认识的几个在 HoloLens 上工作的人是:

    最佳 HTTP

    https://www.assetstore.unity3d.com/en/#!/content/10765

    Uniweb

    https://www.assetstore.unity3d.com/en/#!/content/40505

    资产商店里还有很多其他的,但这是我在 HoloLens 上尝试过的两个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-20
      • 1970-01-01
      • 2016-01-08
      • 2019-12-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多