コード例 #1
0
        private IEnumerator LoadItemsRoutine(ItemsLoaderRequest loaderRequest, Action <ItemsLoaderResponse> callback)
        {
            using (var request = UnityWebRequest.Get(loaderRequest.Url))
            {
                yield return(request.SendWebRequest());

                bool isError = request.isHttpError || request.isNetworkError;

                if (isError)
                {
                    callback(new ItemsLoaderResponse(isError, request.error));
                    yield break;
                }

                string json = Encoding.UTF8.GetString(request.downloadHandler.data);

                Item[] items;
                try
                {
                    items = JsonConvert.DeserializeObject <Item[]>(json)
                            .Take(loaderRequest.RequestedItemsCount)
                            .ToArray();
                }
                catch (JsonException)
                {
                    callback(new ItemsLoaderResponse(true, "JSON data has wrong format"));
                    yield break;
                }
                var icons             = new Dictionary <string, Texture2D>();
                var iconLoadingErrors = new Dictionary <string, string>();
                foreach (var item in items)
                {
                    yield return(LoadIconRoutine(
                                     item.IconUrl,
                                     texture =>
                    {
                        icons.Add(item.Id, texture);
                    },
                                     errorMessage =>
                    {
                        iconLoadingErrors.Add(item.Id, errorMessage);
                    }
                                     ));
                }
                callback(new ItemsLoaderResponse(isError, items, icons, iconLoadingErrors));
            }
        }
コード例 #2
0
 public Coroutine LoadItemsAsync(ItemsLoaderRequest loaderRequest, Action <ItemsLoaderResponse> callback)
 {
     return(StartCoroutine(LoadItemsRoutine(loaderRequest, callback)));
 }