Search Unity

Loading UI.Image by WWW class?

Discussion in 'UGUI & TextMesh Pro' started by Tiles, Aug 21, 2014.

  1. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,481
    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.
    Code (JavaScript):
    1.  
    2. var mypreviewimage:UI.Image;
    3. mypreviewimage.SourceImage=www.texture as UI.Image;
    4.  
    While this doesn't give me any error. But it doesn't load the image neither:
    Code (JavaScript):
    1.  
    2. var mypreviewimage:UI.Image
    3. mypreviewimage=www.texture as UI.Image;
    4.  
     
  2. ortin

    ortin

    Joined:
    Jan 13, 2013
    Posts:
    221
    www.texture is Texture2D, create GO with RawImage component and set its texture;
    RawImage ri = new GameObject("Image").AddComponent<RawImage>();
    ri.texture = www.texture;
     
    Tim-C likes this.
  3. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,481
    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#
     
    Last edited: Aug 21, 2014
  4. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,481
    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.

     
    Last edited: Aug 21, 2014
  5. ortin

    ortin

    Joined:
    Jan 13, 2013
    Posts:
    221
    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.
     
    yashpal and Tiles like this.
  6. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,481
    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 :)
     
    Last edited: Aug 21, 2014
    yashpal likes this.
  7. ortin

    ortin

    Joined:
    Jan 13, 2013
    Posts:
    221
    Here is component 2014-08-21 20_17_29-Unity.png
    Here is texture property 2014-08-21 20_19_16-Microsoft Visual Studio.png
     
    Tiles likes this.
  8. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,481
    Cross posting. Got it working with your help. Not my best day for coding it seems ^^

    And again thanks for your help :)
     
  9. mrekuc

    mrekuc

    Joined:
    Apr 17, 2009
    Posts:
    116
    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.

    Code (CSharp):
    1. private string url = "http://www.somewebsite.com/images/someImage.jpg";
    2. public GameObject myImageGO;  // This is the GameObject with the Image Component on it
    3.  
    4. //In Start or where ever are going to call to get and load the image use:
    5.  
    6. void Start()
    7. {
    8.         StartCoroutine(LoadImage(url));
    9. }
    10.  
    11. private IEnumerator LoadImage(string loadedURL)
    12. {
    13.     Texture2D temp = new Texture2D(0,0);
    14.     WWW www = new WWW(loadedURL);
    15.     yield return www;
    16.  
    17.     temp = www.texture;
    18.     Sprite sprite = Sprite.Create(temp, new Rect(0,0,temp.width, temp.height), new Vector2(0.5f,0.5f));
    19.     Transform thumb = myImageGO.transform;
    20.     thumb.GetComponent<Image>().sprite = sprite;
    21.     }