Пример #1
0
        public IEnumerator Download(string url, string filename, Action <DownloadResponse <Texture2D> > result = null, Action <Texture2D> ifExists = null)
        {
            string localPath = Path.Combine(Application.persistentDataPath, filename.LastSplit());

            if (File.Exists(localPath))
            {
                ifExists?.Invoke(localPath.ToTexture2D());
            }
            else
            {
                Downloading = true;

                using (UnityWebRequest uwr = UnityWebRequestTexture.GetTexture(url)) {
                    yield return(uwr.SendWebRequest());

                    if (uwr.HasErrors())
                    {
                        Print.Log(() => "yellow", $"download error: {uwr.error}, url {url}");

                        result?.Invoke(new DownloadResponse <Texture2D>(null, uwr));
                    }
                    else
                    {
                        // save the texture
                        Texture2D texture = DownloadHandlerTexture.GetContent(uwr);

                        File.WriteAllBytes(localPath, filename.Contains("png") ? texture.EncodeToPNG() : texture.EncodeToJPG());
                        result?.Invoke(new DownloadResponse <Texture2D>(texture, uwr));
                    }

                    Downloading = false;
                }
            }
        }
Пример #2
0
 public DownloadResponse(T d, UnityWebRequest uwr)
 {
     Data         = d;
     Error        = uwr.error;
     ResponseCode = uwr.responseCode;
     IsError      = uwr.HasErrors();
 }
Пример #3
0
        public IEnumerator Download(string url, Action <DownloadResponse <T> > result)
        {
            Downloading.Next(true);

            using (UnityWebRequest uwr = UnityWebRequest.Get(url)) {
                yield return(uwr.SendWebRequest());

                if (uwr.HasErrors())
                {
                    result(new DownloadResponse <T>(default(T), uwr));
                }
                else
                {
                    // save
                    string text = DownloadHandlerBuffer.GetContent(uwr);
                    T      json = JsonUtility.FromJson <T>(text);
                    result(new DownloadResponse <T>(json, uwr));
                }

                Downloading.Next(false);
            }
        }