Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Make String URL Populate Multiple Images In Scroll List?

Discussion in 'Scripting' started by ErmergerdEnt, Sep 25, 2019.

  1. ErmergerdEnt

    ErmergerdEnt

    Joined:
    Apr 7, 2017
    Posts:
    54
    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

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class ImageLoader : MonoBehaviour
    7. {
    8.     public Image image_1;
    9.  
    10.     string url = "My.png";
    11.  
    12.     void Start()
    13.     {
    14.         StartCoroutine(loadSpriteImageFromUrl(url));
    15.     }
    16.     IEnumerator loadSpriteImageFromUrl(string URL)
    17.     {
    18.         WWW www = new WWW(url);
    19.        while(!www.isDone)
    20.         {
    21.             Debug.Log("Download image in progress" + www.progress);
    22.             yield return null;
    23.         }
    24.      
    25.        if(!string.IsNullOrEmpty(www.error))
    26.         {
    27.             Debug.Log("Download Failed");
    28.         }
    29.        else
    30.         {
    31.             Debug.Log("Download Success");
    32.             Texture2D texture = new Texture2D(1, 1);
    33.             texture.LoadImage(www.bytes);
    34.             texture.Apply();
    35.  
    36.             Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
    37.             image_1.sprite = sprite;
    38.         }
    39.      
    40.     }
    41. }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    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
     
    ErmergerdEnt likes this.
  3. ErmergerdEnt

    ErmergerdEnt

    Joined:
    Apr 7, 2017
    Posts:
    54
    Yeah, i changed my code twice and left the string lol. Thanks for pointing that out and thanks for the help!
     
  4. babji3

    babji3

    Joined:
    May 28, 2015
    Posts:
    179
    Hi, can you share me your code, i need that, thx