private IEnumerator request_user_image(string login, fetch_twitch_user_image_callback callback) { string url = _twitch_user_cache[login].profile_image_url; UnityWebRequest texture_request = UnityWebRequestTexture.GetTexture(url); texture_request.SetRequestHeader("Client-ID", "nm92v8wkvmrhulyqrhfct9vljid72k"); yield return(texture_request.SendWebRequest()); if (texture_request.isNetworkError || texture_request.isHttpError) { Debug.Log(texture_request.error); } else { Texture2D texture = ((DownloadHandlerTexture)texture_request.downloadHandler).texture; _twitch_user_image_cache[login] = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(.5f, .5f)); callback(_twitch_user_image_cache[login]); // Store the file locally byte[] img = texture.EncodeToPNG(); string path = Application.persistentDataPath + "/runners/" + login + "/runner_thumb.png"; Directory.CreateDirectory(Path.GetDirectoryName(path)); File.WriteAllBytes(path, img); } }
public void fetch_runner_image(string login, fetch_twitch_user_image_callback callback) { if (_twitch_user_image_cache.ContainsKey(login)) { Debug.Log("Runner Thumb[" + login + "]: image in cache, return it..."); callback(_twitch_user_image_cache[login]); } else if (_twitch_user_cache.ContainsKey(login)) { Debug.Log("Runner Thumb[" + login + "]: not in image cache, check on disk..."); string path = Application.persistentDataPath + "/runners/" + login + "/runner_thumb.png"; if (File.Exists(path)) { Debug.Log("Runner Thumb[" + login + "]: On disk, load it and cache it for reuse!"); byte [] data = File.ReadAllBytes(path); Texture2D tex = new Texture2D(2, 2); tex.LoadImage(data); _twitch_user_image_cache[login] = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(.5f, .5f)); callback(_twitch_user_image_cache[login]); } else { Debug.Log("Runner Thumb[" + login + "]: Model cached, but not image, need to fetch"); StartCoroutine(request_user_image(login, callback)); } } else { // no cache exists, so need to cache both image and model! Debug.Log("Runner Thumb[" + login + "]: not in image or model -cache..."); string path = Application.persistentDataPath + "/runners/" + login + "/runner_thumb.png"; if (File.Exists(path)) { Debug.Log("Runner Thumb[" + login + "]: On disk, load it and cache it for reuse!"); byte [] data = File.ReadAllBytes(path); Texture2D tex = new Texture2D(2, 2); tex.LoadImage(data); _twitch_user_image_cache[login] = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(.5f, .5f)); callback(_twitch_user_image_cache[login]); } else { Debug.Log("Runner Thumb[" + login + "]: not on disk, not in model and not in image cache, need to refetch all"); StartCoroutine(request_user(login, (UserModel m) => { _twitch_user_cache[login] = m; StartCoroutine(request_user_image(login, callback)); })); } } }