Make String URL Populate Multiple Images In Scroll List?

Instead of make 100 scripts for 100 different images im pulling from a URL, i want to make it be able to pull all 100 at a single load time and populate that inside of an index. Is this possible? This is my code as of now. This works fine for single image use, but i cant figure out multi use. Thanks

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ImageLoader : MonoBehaviour
{
    public Image image_1;

    string url = "My.png";

    void Start()
    {
        StartCoroutine(loadSpriteImageFromUrl(url));
    }
    IEnumerator loadSpriteImageFromUrl(string URL)
    {
        WWW www = new WWW(url);
       while(!www.isDone)
        {
            Debug.Log("Download image in progress" + www.progress);
            yield return null;
        }
      
       if(!string.IsNullOrEmpty(www.error))
        {
            Debug.Log("Download Failed");
        }
       else
        {
            Debug.Log("Download Success");
            Texture2D texture = new Texture2D(1, 1);
            texture.LoadImage(www.bytes);
            texture.Apply();

            Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
            image_1.sprite = sprite;
        }
      
    }
}

I find it odd you’re passing in a string to your coroutine, but you’re not actually using that in the coroutine. But, that aside…

You’ll need to just have a list of image names you can access to pull them, or if your images follow a pattern, you just have the base name (image1, image2, image3, etc…).

Then you just have to loop through. In your coroutine, you grab the first image, once it’s downloaded, you apply it to your first image object (have an array of images that you need to apply to).

Then you continue the loop, making sure you have proper error handling in case an image doesn’t get downloaded.

//Collection of file names
//Collection of image targets

//Create coroutine with for loop
//As each image is downloaded, apply to appropriate image target, handle if image fails to download

1 Like

Yeah, i changed my code twice and left the string lol. Thanks for pointing that out and thanks for the help!

Hi, can you share me your code, i need that, thx