Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by Rotavele · May 25, 2016 at 11:06 AM · c#scripting problemuiguiui image

Using downloaded image as UI Image's source image?

This has been bothering me for awhile and I can't figure it out.

Basically what the IEnum of the script does, and what I know how to do:

  1. Use a WWW to get a list of image names returned to it via PHP. [Yield]

  2. Split the Names list into an array.

  3. Count the array

  4. For loop to instantiate a button prefab of a button object with an image child for each image. [Yield]

  5. Use another for loop and WWW again to download each image. [Yield]

That's where I get stuck. Step 6 would be to load the downloaded images with the IEnumerator to the source image of each UI.Image.

I've seen a few questions on this and did my homework on it for awhile now. None of them address downloading the images, but rather loading them from the resources folder. I know that I could use OnGUI to do it easily, but most of the answers of my previous questions has warned against using OnGUI.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by col000r · May 26, 2016 at 04:46 PM

All you need to do is load the downloaded image into a texture and then create a sprite from that texture:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class DownloadAndDisplay : MonoBehaviour {
 
     public Image img;
 
     IEnumerator Start () {
         WWW www = new WWW("http://gameassets.net/GameAssetsLogo.png");
         yield return www;
         img.sprite = Sprite.Create( www.texture, new Rect( 0f, 0f, www.texture.width, www.texture.height ), new Vector2( 0.5f, 0.5f ) );
         img.SetNativeSize();
     }
 
 }

If you use UI.RawImage instead of UI.Image it's even easier, because then you can just assign the Texture directly and don't have to turn it into a Sprite:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class DownloadAndDisplay : MonoBehaviour {
 
     public RawImage img;
 
     IEnumerator Start () {
         WWW www = new WWW("http://gameassets.net/GameAssetsLogo.png");
         yield return www;
         img.texture = www.texture;
         img.SetNativeSize();
     }
 
 }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Rotavele · May 27, 2016 at 07:14 PM 0
Share

Brilliant. Thank you so much!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

182 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

UnityEngine.UI and UnityEngine.EventSystem do not work, what do I do? 1 Answer

Better way to edit RawImage texture data?,Better way to edit a RawImage texture? 0 Answers

Android joystick doesn't work after 2nd level >.> 2 Answers

RectTransform Left Right Bottom Top 0 Answers

How Can i Get This bool to work? Just need someone willing to explain. 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges