- Home /
How to load resources from www in loading scene time?
Hi, the pictures and the texts that we are using in our game come from a server. Is there any way to load them while scene is loading? Because we don't want to show the rest of the scene while the main things are not downloaded.
Unity has the WWW class for accessing files on the internet.
Answer by psycocrusher · Feb 22, 2016 at 07:42 PM
You need to use yield return www
public class RequestFromServer : MonoBehaviour {
IEnumerator Start() {
//Get an image from the server.
WWW www = new WWW("YourUrl.jpg");
yield return www;
if (www.isDone && www.error == null) {
//Load level
}
}
Answer by Magius96 · Feb 22, 2016 at 07:26 PM
You'll want to look into the WWW class. I've done this for a work project. The idea is to load the image from the url into memory, then convert it into what ever you need, most likely a Texture. This does introduce the issue that it wont necessarily all load at once, so I advise writing your scene such that it displays a loading bar until all your assets are loaded. Here's the code I used:
WWW www = new WWW("your image url here");
StartCoroutine(DownloadImage(www));
private IEnumerator DownloadImage(WWW www)
{
yield return www;
Texture2D img = new Texture2D(1, 1);
www.LoadImageIntoTexture(img)
// In case you want to save the image
byte[] bytes = img.EncodeToPNG();
File.WriteAllBytes("a file name.png", bytes);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612065144im_/https://answers.unity.com/themes/thub/images/avi.jpg)