Reputation: 461
I'm using Parse SDK for Unity to get some files and save on my resources directory. When I call parse.findAsync() it creates another task and its impossible for me to call WWW to download the URL I got from parse. I need to:
I tried this:
public IEnumerator GetXXXAsync(String objectId){
var query = ParseObject.GetQuery("xxx").WhereEqualTo("yyy", ParseObject.
CreateWithoutData("zzz", objectId));
List<String> urlList = new List<String>();
Album album = null;
query.FindAsync().ContinueWith(t =>
{
var tasks = new List<Task>();
Task task = Task.FromResult(0);
foreach (var result in t.Result)
{
ParseObject obj = result;
ParseFile file = (ParseFile)obj.Get<ParseFile>("image");
if(file != null){
urlList.Add(file.Url.ToString());
tasks.Add(GetImageAsync(file.Url.ToString(), "image.png"));
}
}
// finished.
return Task.WhenAll(tasks);
}).Unwrap().ContinueWith(_ =>
{
gameManager.SendOk();
});
return null;
}
private WWW WaitForImage(String url, string filename) {
WWW www = new WWW( url );
while(!www.isDone){
Debug.Log("Waiting");
}
Utils.SaveFileFromTexture(www.texture, (gameManager.GetResourcesPath() + "/Textures/" + filename));
Debug.Log("Saving file ");
return www;
}
public Task<WWW> GetImageAsync(String url, string filename) {
var task = new TaskCompletionSource<WWW>();
Debug.Log("GetImageAsync " + filename);
task.SetResult(WaitForImage(url, filename));
return task.Task;
}
I Tried to get a IEnumerator instead of a WWW, but I always get WWW "could not be called outside main thread". Is there anything I can do to call WWW outside main thread? Or anything else?
Tks,
Upvotes: 2
Views: 679
Reputation: 1085
You could use this to Run WWW on the main thread after your async stuff is done. Or you could use webclient in the system.net to download on a background thread. Dont forget you can yield the download with WWW on the main thread to make it not lockup your frames while downloading.
webclient example
WebClient webClient = new WebClient();
webClient.DownloadProgressChanged += OnChange;
webClient.DownloadFileCompleted += OnCompleted;
webClient.DownloadFileAsync(new Uri(download), fileAndPath);
Upvotes: 1