Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by justinpatterson · Feb 13, 2014 at 06:19 AM · texturecoroutinewwwdownload

WWW class downloading image doesn't work in Web Player?

Hi all,

I'm fiddling with downloading images from a folder on my server to an array in Unity. It works in the editor and in .exe / .app builds, but not in the web build version. Any ideas? I'll post relevant code below. I suspect it has something to do with improper coroutining, since I know nothing (basically) about coroutines.

 var imageURLString:String="";
 var isLoading:boolean=false;
 private var textureArray = new Array();
 
 
 function Start(){
     GetImageList("myurl.php");
     }
 
 
 function GetImageList(url:String){
        //returns a string of images separated by semi-colons like this:
        //username,imageurl; username,imageurl; etc. etc.
     isLoading = true;
     textureArray = [];
     Debug.Log("getting it");
     var www : WWW = new WWW (url);
     yield www;
     if(www.error){Debug.Log("Error is " + www.error);
         returnString = www.error;
         }
     else {
         imageURLString = www.text;
         //returnString = www.text;
         }
         //now I send that string with ; and , separations to "Download Images"
     DownloadImages(imageURLString);
     }
 
 function DownloadImages(str:String){
     if(str.Length<=1) return;
     //break the string up into [user+imgurl, user+imgurl, ... ]
     var imageStrings:String[] = str.Split(","[0]);
     for(var s in imageStrings){
         //break string up more, into [[user, imgurl], [user, imgurl], ...]
         var currArr : String[] = s.Split(";"[0]);
         var user = currArr[0];
         var url = currArr[1];
         // for each image, grab the image from the imgurl and pass the username too.
         GetImageFromURL(url, user);
         }
     isLoading = false;
     }
 
 
 function GetImageFromURL (url:String, usr:String) {
     // Start a download of the given URL
     Debug.Log(url);
     var www : WWW = new WWW (url);
     // Wait for download to complete
     
         while(!www.isDone){
         yield;
         }
     //yield www;
     // assign texture;
     returnString = www.error;
     if(www.error) return;
     try{
         var t:Texture;
         try{
             t = www.textureNonReadable;
             }
         catch(err){
             Debug.Log(err);
             returnString=err.ToString();
             }        
         //FINALLY: push the username and the texture into an array.
         textureArray.push([usr, t]);
         }
     catch(err) {
         returnString = err.ToString();
         }
     
     }

It's pretty much been comically frustrating. I've been messing around with coroutining different areas but it doesn't seem to give. Any ideas?

EDIT: I'll note that the error I get during this process I pass to a GUI element, and it says "Failed downloading url.com/images/image.jpeg"

Comment
Add comment · Show 5
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 fafase · Feb 13, 2014 at 06:26 AM 0
Share

We have had the same issue even using the example from the docs for downloading videos. It works most of the time on the editor but would take ages on the web player and would not play the videos even though they are downloaded. I have looked around for a solution to this question but it has never been answered. So I think we are facing a case of fixing on our own which is usual when you ask something that is not a null reference exception pb...

avatar image justinpatterson · Feb 13, 2014 at 01:31 PM 0
Share

Ah at least that confirms it's not just me! There's gotta be some sort of solution! I'll keep tooling around.

avatar image justinpatterson · Feb 13, 2014 at 03:03 PM 0
Share

I think I have a word-around in $$anonymous$$d I'm going to try to implement today and I'll keep you updated.

Before: in the way it is now, I use WWW to access PHP for a list of URLs from a server. Then I use the WWW class in Unity to try to grab the images from bytes, which borks in Web Build. After: Now, I'll first use the WWW to access PHP for a list of URLs from a server just like before, BUT - once I have those URLs, I'll pass them to a WWW PHP form and have PHP return the bytes. That way, the conversion process is server-side and may cut just enough time off the whole process to not error out. I've had no problem with text-based PHP grabbing with WWW so far so it might work.

avatar image Graham-Dunnett ♦♦ · Feb 13, 2014 at 03:57 PM 0
Share

See http://docs.unity3d.com/Documentation/$$anonymous$$anual/SecuritySandbox.html - I don't think you can access the bytes of the image.

avatar image justinpatterson · Feb 13, 2014 at 05:15 PM 0
Share

I solved it :) posting the solution now. EDIT: @GrahamDunnet I think you're probably right. Perhaps the easier solution would have been adding a cross domain xml, though it's not really necessary anymore with the solution below.

1 Reply

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

Answer by justinpatterson · Feb 13, 2014 at 05:21 PM

Hey everyone,

I went forward with my solution I mentioned in the comments section of my question. The following fixed my issue:

STEP 1: PHP Back-end

So I needed to, instead of reading files directly online, set up PHP to report the byte array of each image.

 <?php
 header('Content-type: image/jpeg');
 if ($_POST) 
 {
     if ( isset ($_POST['action']) ) 
     {
         if($_POST['action'] === 'gallery download') 
         { 
             $file = $_POST['file'];
             $path = 'images/uploads/' . $file;
             $type = pathinfo($path, PATHINFO_EXTENSION);
 
             $img = fopen($path, 'rb');
             $imgEncode = file_get_contents($path, FILE_USE_INCLUDE_PATH);
             echo $imgEncode;
         }
     }
 }
 ?>

STEP 2: Modifying the javascript Unity-side

Super minimal changes are necessary :) All changes needed were made to GetImageFromURL

 function GetImageFromURL (file:String, usr:String) {
      // Start a download of the given URL
     Debug.Log(file);
     var url:String = "myPHPurl.com/GetImageBytes.php";
     Debug.Log(url);
     
     //CHANGE 1: PASS DESIRED IMAGE URL TO A FORM
     var form = new WWWForm();
     form.AddField("action", "gallery download");
     form.AddField("file", file);
     
     var www : WWW = new WWW (url, form);
     // Wait for download to complete
     yield www;
     
     // assign texture;
     try{
         //CHANGE 2: READ THE TEXTURE FORMATTED BYTE ARRAY RETURNED BY THE FORM
         var byteTexture = www.texture;
         textureArray.push([usr, byteTexture]);
         }
     catch(err) {
         }
     }
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 DeveshPandey · Oct 28, 2014 at 03:16 PM 0
Share

You can use this plugin http://u3d.as/9Ee

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

19 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

Related Questions

How to load interlaced streamed image into a texture? 0 Answers

WWW 16 bits textureformat download possible? 2 Answers

Can I access previously downloaded textures? 2 Answers

www.texture textureType to GUI 0 Answers

Ios www.texture returns question mark 1 Answer


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