Loading a sprite from URL C#

I’m trying to load an image from an URL, but none of the answers i found online apply to my problem. There is a good example in the unity docs, but it’s javascript:

I than need to load this sprite onto UI image element.

Please help,

Thanks

Solved thanks to the The Little Guy from unity forums!

`

   public class ExampleClass : MonoBehaviour {
 
    // The output of the image
    public Image img;
 
    // The source image
    public string url = "http://images.earthcam.com/ec_metros/ourcams/fridays.jpg";
 
    IEnumerator Start() {
        WWW www = new WWW(url);
        yield return www;
        img.sprite = Sprite.Create(www.texture, new Rect(0, 0, www.texture.width, www.texture.height), new Vector2(0, 0));
    }
}

`

I wonder if there is any option to auto scale the aspect ratio of the downloaded image? In my case there is a placeholder with 800x600 pixels. So I want to keep the downloaded image’s aspect ratio but cap its size to 800x600? Right now unity tries to fill my placeholder to its max…

EDIT: Ok found the solution by my self: At first I had not any source file in my image object. This caused the “preserve aspect ratio” button to not appear. After I inserted a placeholder image (before uploading one) the button appeared. Then just simply turn it on, now every time you upload a new image from url it keeps its aspect ratio! :slight_smile:

Instead of Sprite.Create, you may want to use WWW.LoadImageIntoTexture to ensure your image isn’t downsampled by QualitySettings (the difference is very noticeable at “Eighth Res”).

www.LoadImageIntoTexture(img.mainTexture as Texture2D);

Unity-ImageLoader
I made the package for loading images from remote or local destinations. It already has integrated two levels of the cache system. Each cache level is configurable (enable/disable).

Usage

var sprite = await ImageLoader.LoadSprite(imageURL);

Installation

openupm add extensions.unity.imageloader