Search Unity

Download Images From Web Server & Store

Discussion in 'Scripting' started by siddharth3322, Dec 18, 2020.

  1. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    1,049
    I want to download 29 background images from a web server and store them within the local storage of the device.
    I want to upload all backgrounds on my webserver because my device builds size not get increased so I want to provide the functionality to download them later on when the game gets opened by the player.

    At present, I can able to create a demo project that can load and save one image from the server.
    Here is the code snippet that I am using:

    Load Texture from Web URL
    Code (CSharp):
    1. // enumerator to load texture from web URL
    2.     IEnumerator LoadTextureFromWeb()
    3.     {
    4.         UnityWebRequest www = UnityWebRequestTexture.GetTexture(imageURL);
    5.         yield return www.SendWebRequest();
    6.  
    7.         if (www.isNetworkError || www.isHttpError)
    8.         {
    9.             Debug.LogError("Error: " + www.error);
    10.         }
    11.         else
    12.         {
    13.             Texture2D loadedTexture = DownloadHandlerTexture.GetContent(www);
    14.             textureImage.sprite = Sprite.Create(loadedTexture, new Rect(0f, 0f, loadedTexture.width, loadedTexture.height), Vector2.zero);
    15.             textureImage.SetNativeSize();
    16.         }
    17.     }
    Save Texture to Device Storage
    Code (CSharp):
    1.  private void WriteImageOnDisk()
    2.     {
    3.         byte[] textureBytes = textureImage.sprite.texture.EncodeToPNG();
    4.         File.WriteAllBytes(Application.persistentDataPath + fileName, textureBytes);
    5.         Debug.Log("File Written On Disk!");
    6.     }
    This is just a code to deal with only one image but I want to run a process for 29 background images.
    How to do that?
    Also, I want to put a loading bar when these images are downloading so give me guidance in this too.
    Thank you for your time given to read this question :)
     
    khanhabib likes this.
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    Put the names in a string array and iterate it and download them, either one at a time or all at once.

    Each ongoing download has a progress field you can check from another coroutine while you are yielding. How you aggregate those into a single loading progress bar is up to you. You might need to store a manifest for all files to say how large they are and use that as a fraction, or just assume each file is the same, up to you. Loading bars are VERY hard to get right in the general case, when you don't know what work is being done or how much there is to do.
     
    siddharth3322 and Bunny83 like this.
  3. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    1,049
    So it's just a loop that I have to implement 29 times with the same code or anything else?

    what is in the loop one or more images get failed with some network problem?
    what is the proper way to deal with this?

    Thank you, Sir, for sharing your opinion :)
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    Probably by working through some tutorials on looping. There's plenty out there, I'm not going to type one in for you.
     
    agpatel0007 likes this.
  5. khanhabib

    khanhabib

    Joined:
    Oct 11, 2018
    Posts:
    29
    Hello Sir
    i am Unity beginner please guide me how to make workable your code?
    what is 'fileName' in second function?
    Thanks
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    Replace it with whatever you want the filename to be. That variable is almost certainly just a
    private string fileNAme;
    that has been preloaded with the filename.
     
    khanhabib likes this.
  7. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    1,049
    I have made a complete video for this topic:
     
  8. khanhabib

    khanhabib

    Joined:
    Oct 11, 2018
    Posts:
    29
    Thank you so much all
     
    siddharth3322 and Kurt-Dekker like this.
  9. aakashsolanki890

    aakashsolanki890

    Joined:
    Jan 13, 2021
    Posts:
    3
    If someone is still looking to load an image from the server, You can Use this code.

    Code (CSharp):
    1. public IEnumerator LoadImageFromURL(string url)
    2.         {
    3.             UnityWebRequest www = UnityWebRequestTexture.GetTexture(url);
    4.             yield return www.SendWebRequest();
    5.             if (www.isNetworkError || www.isHttpError)
    6.             {
    7.                 Debug.LogError(www.error);
    8.             }
    9.             else
    10.             {
    11.                 Texture2D texture = ((DownloadHandlerTexture)www.downloadHandler).texture;
    12.                 Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2());
    13.                 yourImage.sprite = sprite;
    14.             }
    15.         }
    This code will work in both ways to load a sprite or texture, you can also refer to this article for more information:- https://gamedevsolutions.com/load-image-from-url-in-unity/
     
    noobogami likes this.
  10. diego_poveda

    diego_poveda

    Joined:
    Jan 25, 2021
    Posts:
    6
    Thank you friend very clear and effective your information
     
    siddharth3322 likes this.
  11. gunma0099

    gunma0099

    Joined:
    Jun 29, 2023
    Posts:
    1
    I ran into an issue in unity I am Putting together a world for vrchat But all the black and white images I am trying to link load up Red tinted is there's a way to fix that
     
  12. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    Sounds like you better hurry up and fix that issue before you run into it again!

    Necro posting will not fix the issue. Start your own post... it's FREE!

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, log output, variable values, and especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

    The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven't put effort into finding the documentation, why should we bother putting effort into replying?


    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/