Advertisement
Wolverine_X-Man

DownloadMultipleImageFromGoogleDrive.cs

Mar 3rd, 2021
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.81 KB | None | 0 0
  1. using System.Collections;
  2. using TMPro;
  3. using UnityEngine;
  4. using UnityEngine.Networking;
  5. using UnityEngine.UI;
  6.  
  7. public class DownloadMultipleImageFromGoogleDrive : MonoBehaviour
  8. {
  9.     [SerializeField] private TMP_Text imageID;
  10.     [SerializeField] private TMP_Text imageName;
  11.     [SerializeField] private RawImage rawImage;
  12.     [SerializeField] private PhotoList photoList;
  13.  
  14.     private string JsonURL = "https://drive.google.com/uc?export=download&id=1WrQGpdb53_UoUxeyWCOHE-XB8ujWLS7R";
  15.     void Start()
  16.     {
  17.         StartCoroutine(GetJsonFile());
  18.     }
  19.  
  20.     IEnumerator GetJsonFile()
  21.     {
  22.         UnityWebRequest webRequest = UnityWebRequest.Get(JsonURL);
  23.         yield return webRequest.SendWebRequest();
  24.         if (webRequest.result != UnityWebRequest.Result.Success)
  25.         {
  26.             Debug.LogError("Json File Not Download : "+ webRequest.error);
  27.         }
  28.         else
  29.         {
  30.             photoList = JsonUtility.FromJson<PhotoList>(webRequest.downloadHandler.text);
  31.             foreach (Photo photo in photoList.Photos)
  32.             {
  33.                 imageID.text = photo.ImageID;
  34.                 imageName.text = photo.ImageName;
  35.                 StartCoroutine(GetImage(photo.ImageURL));
  36.                 yield return new WaitForSeconds(5);
  37.             }
  38.         }
  39.     }
  40.  
  41.     IEnumerator GetImage(string imageURL)
  42.     {
  43.         UnityWebRequest webRequestTexture = UnityWebRequestTexture.GetTexture(imageURL);
  44.         yield return webRequestTexture.SendWebRequest();
  45.         if (webRequestTexture.result != UnityWebRequest.Result.Success)
  46.         {
  47.             Debug.LogError("Image Not Download : "+webRequestTexture.error);
  48.         }
  49.         else
  50.         {
  51.             rawImage.texture = ((DownloadHandlerTexture) webRequestTexture.downloadHandler).texture;
  52.         }
  53.     }
  54.    
  55. }
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement