Search Unity

Downloading textures into array

Discussion in 'Scripting' started by GeneralDollar, Aug 21, 2018.

  1. GeneralDollar

    GeneralDollar

    Joined:
    Mar 24, 2018
    Posts:
    27
    Hey everyone! I am not sure if this is the right section to ask this.

    I am attempting to download textures from an array of URLs and then place the downloaded textures into a texture array.

    Code (CSharp):
    1.  
    2.     public Texture2D tex;
    3.     public Texture2D[] gallery;
    4.  
    5.  
    6.     IEnumerator GetTexture(string urlLi)
    7.     {
    8.  
    9.         Debug.Log("Start Download");
    10.         WWW www = new WWW(urlLi);
    11.         while (!www.isDone)
    12.             yield return null;
    13.         Debug.Log(www.texture.name);
    14.         Debug.Log("Got Picture");
    15.         www.LoadImageIntoTexture(tex);
    16.  
    17.         if (gallery[index] == null)
    18.         {
    19.             gallery[index] = tex;
    20.         }
    21.         else
    22.         {
    23.             for (index = 0; index < gallery.Length; ++index)
    24.             {
    25.                 if (gallery[index] == null)
    26.                 {
    27.                     gallery[index] = tex;
    28.                     break;
    29.                 }
    30.             }
    31.  
    32.         }
    33. }

    I am sure this is probably the most inefficient way of doing this, but I am still fairly new to all of this. Any help and assistance would be much appreciated!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    This seems like it should work. Does it not? How does it fail? Do you get any picture? Can you get even one picture explicitly (i.e., make sure the URL is right)?

    Also, for simplification, I think you could simplify it by deleting lines 17 through 22 and line 32: the for loop will already iterate over your gallery and find the first free slot regardless of index, which also does not have to be a member variable, just declare it in the header of the for loop and make it a local instead.
     
  3. GeneralDollar

    GeneralDollar

    Joined:
    Mar 24, 2018
    Posts:
    27
    Being impatient, I already rearranged absolutely everything to make it work lol. It no longer creates an array in the IEnumerator. It now passes each URL individually as it is needed outside of the coroutine, which seems like it would suck up a ton of data doing it like this, but I am not an expert with this.

    New Code:
    Code (CSharp):
    1.     IEnumerator GetTexture(string urlLi)
    2.     {
    3.  
    4.         Debug.Log("Start Download");
    5.         WWW www = new WWW(urlLi);
    6.         while (!www.isDone)
    7.             yield return null;
    8.         Debug.Log(www.texture.name);
    9.         Debug.Log("Got Picture");
    10.         www.LoadImageIntoTexture(tex);
    11.         GameObject.Find("List").GetComponent<RawImage>().texture = tex;
    12. }
    I would love to get the other code to work if I can. It appears to me that it would download everything just the first time and then allow me to use the textures freely after. Is that correct?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    It appears to me too.

    How does it appear to Unity when you run it? :)
     
  5. GeneralDollar

    GeneralDollar

    Joined:
    Mar 24, 2018
    Posts:
    27
    With the first code, it appears to keep loading both images under [0]. Then if I try and cycle through the array, I receive array index is out of range. The texture[] is set to the size of the array coming in to the IEnumerator, so I am not sure why this is not holding true all the way through. I did not have this problem with the second code.

    I hope this makes sense to you somehow!