Loading UI.Image by WWW class?

I load a image by www class. It arrives fine in the material. But i cannot figure out how to load it into a UI Image.

This gives me a error message.

var mypreviewimage:UI.Image;
mypreviewimage.SourceImage=www.texture as UI.Image;

While this doesn’t give me any error. But it doesn’t load the image neither:

var mypreviewimage:UI.Image
mypreviewimage=www.texture as UI.Image;

www.texture is Texture2D, create GO with RawImage component and set its texture;
RawImage ri = new GameObject(“Image”).AddComponent();
ri.texture = www.texture;

1 Like

Thanks ortin,

Unfortunately this doesn’t work it seems. Unity doesn’t know a componente called RawImage. Unknown Identifier in JS, and type or namespace could not be found in C#

Okay, i think i have misunderstood what you tried to tell me. So i tried the same with the Raw Image component for UI now. With the same result. This time Unity moans about that it cannot convert to raw image instead of texture 2d:

Curious enough the manual states:

This sounds to me that this should work:

var myrawimage:UI.RawImage;

myrawimage=www.texture;

It doesn’t.

I’m a bit confused, do you understand that Texture2D and RawImage/Image are completely different types?
And again in my code I set myrawimage_.texture_ = www.texture and not just instance of RawImage directly.

2 Likes

Yes i understand that those two are two different types. Guess why i search for a method to convert texture2D to something different that can be read by the UI component.

Problem is your code gives me error. Unity does not know a component called RawImage. And myrawimage has no type called texture. It has raw texture or sprite.

I’m completely confused ^^

EDIT, now i get this part at least. Man, i’m blind today. I need to access the texture component of the script. So easy …

mypreviewimage.texture=www.texture;

Works. Many thanks for your help :slight_smile:

1 Like

Here is component


Here is texture property

1 Like

Cross posting. Got it working with your help. Not my best day for coding it seems ^^

And again thanks for your help :slight_smile:

I know it’s an old thread, but thought I would post this anyway in case it helps anyone. I have been using this way and it works just fine loading URL’s into a sprite.

private string url = "http://www.somewebsite.com/images/someImage.jpg";
public GameObject myImageGO;  // This is the GameObject with the Image Component on it

//In Start or where ever are going to call to get and load the image use:

void Start()
{
        StartCoroutine(LoadImage(url));
}

private IEnumerator LoadImage(string loadedURL)
{
    Texture2D temp = new Texture2D(0,0);
    WWW www = new WWW(loadedURL);
    yield return www;

    temp = www.texture;
    Sprite sprite = Sprite.Create(temp, new Rect(0,0,temp.width, temp.height), new Vector2(0.5f,0.5f));
    Transform thumb = myImageGO.transform;
    thumb.GetComponent<Image>().sprite = sprite;
    }
5 Likes