首页 > 解决方案 > 如何使用 RestClient for Unity 执行多个请求,然后在所有请求完成后执行一个函数?

问题描述

使用这个库https://github.com/proyecto26/RestClient我想从 s3 服务器获取多个图像,然后在它们全部下载后执行一个函数。我有:

RestClient.Get(url).Then( response => {
            byte[] imageBytes = response.Data;
            string pathToSaveImage = "/some/example/local/absolute/path/to/image.jpg";
            File.WriteAllBytes(pathToSaveImage, imageBytes)
        } ).Catch( err => {
            Debug.Log(err.Message);
        } );

但是如果我遍历一个 URL 列表,我怎么知道所有请求何时完成?

C# 和 Unity 的新手。任何帮助表示赞赏!谢谢!

标签: c#restunity3dpromise

解决方案


如果您必须使用“RestClient”,请使用正确的请求来获取照片。您可以使用以下代码:

var currentRequest = new RequestHelper { 
  Uri = "https://jsonplaceholder.typicode.com/photos",
  Headers = new Dictionary<string, string> {
    { "Authorization", "Other token..." }
  },
  Params = new Dictionary<string, string> {
    { "param1", "Other value..." }
  }
};
RestClient.GetArray<Photo>(currentRequest).Then(response => {
  EditorUtility.DisplayDialog("Header", currentRequest.GetHeader("Authorization"), "Ok");
}); 

此外,如果您要下载多张照片,请尝试根据请求获取多张照片的 URL。

您可以通过以下方式了解状态:

currentRequest.UploadProgress; //The progress by uploading data to the server
currentRequest.UploadedBytes; //The number of bytes of body data the system has uploaded
currentRequest.DownloadProgress; //The progress by downloading data from the server
currentRequest.DownloadedBytes; //The number of bytes of body data the system has downloaded
currentRequest.Abort(); //Abort the request manually

推荐阅读