Skip to content

Instantly share code, notes, and snippets.

@hasanbayatme
Forked from maxoja/ImageLoader.cs
Last active October 10, 2018 11:33
Show Gist options
  • Save hasanbayatme/2103b803583ac6f615f92434f254d5be to your computer and use it in GitHub Desktop.
Save hasanbayatme/2103b803583ac6f615f92434f254d5be to your computer and use it in GitHub Desktop.
Unity How to : Load Image from WebLink or URL
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ImageLoader : MonoBehaviour
{
public string url = "https://i.pinimg.com/originals/9e/1d/d6/9e1dd6458c89b03c506b384f537423d9.jpg";
public RawImage rawImage;
// automatically called when game started
void Start()
{
StartCoroutine(LoadFromLikeCoroutine()); // execute the section independently
// the following will be called even before the load finished
rawImage.color = Color.red;
}
// this section will be run independently
private IEnumerator LoadFromLikeCoroutine()
{
Debug.Log("Loading ....");
WWW wwwLoader = new WWW(url); // create WWW object pointing to the url
yield return wwwLoader; // start loading whatever in that url ( delay happens here )
Debug.Log("Loaded");
rawImage.color = Color.white; // set white
rawImage.texture = wwwLoader.texture; // set loaded image
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment