Hey, guys, I’m fairly new to scripting so I apologize if this turns out to be really basic, I am creating a 2d game, where I am creating a singleton be used by any class to download images from the web. I want to Expose a string URL and auto set the image to downloaded image from web.and if If no image is
downloaded because of the internet or other issues, it will default to an existing image. I don’t know how to do it, help me pls thanks in advance!
I created the singleton script that loads the image I attached the same script for 5 images when I call the function it all loads the same image but I want to show diffrent images.
load image code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class downloadimage : MonoBehaviour {
public static downloadimage Instance { get; private set; }
public string url;
public Image img;
private void Awake()
{
if(Instance==null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
}else
{
Destroy(gameObject);
}
}
public void Img1()
{
StartCoroutine(Image1());
}
IEnumerator Image1()
{
WWW www = new WWW(url);
yield return www;
if (www.texture != null)
{
img.sprite = Sprite.Create(www.texture, new Rect(0, 0, www.texture.width, www.texture.height), new Vector2(0, 0));
}
}
}
load code(to call the instance internally to load the image)
using System.Collections;
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class load : MonoBehaviour
{
// The output of the image
public Image img;
// The source image
public void Start()
{
downloadimage.Instance.url = "https://image.shutterstock.com/image-photo/sebechleby-slovakia-january-6-2015-260nw-243797071.jpg";
}
public void Update()
{
downloadimage.Instance.Img1();
}
}