I have made this unity package such that anyone can download it and use it. This is an image downloader package. Download it, drag and drop in unity, drop the prefab in your scene for demo.

The Downloader package consists of two scripts ImageDownloader.cs and WebImage.cs. ImageDownloader.cs has a function called DownloadImage() with the reference of the RawImage, Image ID, bool for Caching Status and image url as parameters. The function first checks if the original cache, if stored,should be deleted. Then after performing the said operation, it calls a GetImage() function which downloads the image and assigns it to the respective image that called the download.

I have made this package just for getting to know the networking library of Unity. I wanted to actually use async and await to download the images in parallel. That requires some more libraries to use the statement await req.SendWebRequest(). Hence i have used coroutines. It was really satisfying to know i implemented this in about 2 hours including researching about it. The exposed variables for this package include URLs, time limit to keep cached images, whether to cache images or not. Note: please use Raw Images in unity to assign your downloaded images


CHECK FOR CACHE DELETE

    private bool ShouldCacheBeDeleted()
    {
        if (PlayerPrefs.HasKey(SAVED_DATE))
        {
            DateTime savedDate = DateTime.Parse(PlayerPrefs.GetString(SAVED_DATE));
            TimeSpan ts = DateTime.Now - savedDate;
            if (ts.Days > numDaysToCacheImages)
            {
                return true;
            }
            return false;
        }
        return false;
    }
          

This function first check if a date for an existing cache has been previously has been stored. If there is a pre stored date means images are already cached. It takes that stored date and compares if (numOfDays) some number of days have passed. If so it will delete that stored cache.

    private void DeleteCache(int id)
    {
      string path = Application.persistentDataPath + "/img" + id;
      File.Delete(path);
    }
              

ACTUAL IMAGE DOWNLOAD

    IEnumerator GetTexture(RawImage imageRef,int id,bool shouldCache, string URL)
    {
        UnityWebRequest www = UnityWebRequestTexture.GetTexture(URL);
        www.timeout = 10;
        yield return www.SendWebRequest();

        if (www.result != UnityWebRequest.Result.Success)
        {
            Debug.Log(www.error);
            Debug.Log("Download Failed");
            urls.Remove(URL);
        }
        else
        {
            PlayerPrefs.SetString(SAVED_DATE,DateTime.Now.ToString());
            Texture2D myTexture = ((DownloadHandlerTexture)www.downloadHandler).texture;

            if(shouldCache)
            {
                CacheImage(www, id, myTexture);
            }
            imageRef.texture = myTexture;
            urls.Remove(URL);
        }
    }
              

This function actually downloads the image. This part had me researching a bit about networking in unity for image downloading. Not really tough to code if you really understand the concept. First a web request is created with the URL and a timeout for the request is set incase the request doesnt respond for a long time. It sends request in that frame and yields. If result is success then we save the date when user first downloaded image and caches image based on the bool parameter.