我正在使用Parse SDK for Unity来获取一些文件并保存在我的资源目录中。当我调用parse.findAsync()时,它会创建另一个任务,我无法调用WWW来下载我从解析中获得的URL。我需要:
我试过了:
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;
}
我试图获得一个IEnumerator而不是WWW,但我总是得到WWW“无法在主线程之外调用”。有什么办法可以在主线程外面调用WWW吗?还是其他什么?
韩国社交协会,
答案 0 :(得分:1)
您可以在异步内容完成后使用this在主线程上运行WWW。或者您可以使用system.net中的webclient在后台线程上下载。不要忘记你可以在主线程上使用WWW进行下载,以免在下载时锁定你的帧。
webclient示例
WebClient webClient = new WebClient();
webClient.DownloadProgressChanged += OnChange;
webClient.DownloadFileCompleted += OnCompleted;
webClient.DownloadFileAsync(new Uri(download), fileAndPath);