I'm building an app in unity (5.4.0f3) for the Windows Store platform (Universal 10 SDK). I'll be running it on hololens.
I am having an issue compiling my scripts and it appears to be due to the WebClient class that I am using. I tried also using HttpClient as recommended in another post but no luck. I have seen some people successfully build using the WebClient class in Unity but I guess they didn't build for the Windows Store.
The compile errors I am getting: error CS0246: The type or namespace name 'WebClient' could not be found (are you missing a using directive or an assembly reference?) The name 'webClient' does not exist in the current context
I have just started working with Unity but I believed that I could add a few directives around the code that uses the WebClient or declares a new WebClient so that it would still compile and be able to run on the hololens.
I found the "Platform dependent Compilation" page (https://docs.unity3d.com/Manual/PlatformDependentCompilation.html) which seemed to explain this.
I tried using a few of them (for example UNITY_WSA, UNITY_WSA_10_0 etc,) but no luck. I'm using the yahoo finance API currently in the following manner: webClient.DownloadFile(url, stockFile);
which downloads a .csv
file.
Any suggestions?
I noticed that you used WebClient.DownloadFile
. You should not use the DownloadFile
function since this will wait block until the file has finished downloading. What happens this time is that the UI will be frozen since the main Thread is blocked.
If you want to use the WebClient
API, the DownloadFileAsync
function should be used. Here is an example of how to do that.
You can use WebClient
on platforms other than Hololens. You can then use WWW
or UnityWebRequest
on hololens then use File.WriteAllBytes
to save the file after it has finished downloading.
The pseudo code should look like this:
#if !UNITY_WSA_10_0
WebClient API
#else
WWW API
#endif
Complete code:
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
Usage:
downloadFile();
If you still get error then you are also using namespace that is not supported on Hololens. You should handle that as-well. Let's that the namepsace is System.Net;
, you can fix it like below:
#if !UNITY_WSA_10_0
using System.Net;
#endif