- Home /
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"
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...
Ah at least that confirms it's not just me! There's gotta be some sort of solution! I'll keep tooling around.
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.
See http://docs.unity3d.com/Documentation/$$anonymous$$anual/SecuritySandbox.html - I don't think you can access the bytes of the image.
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.
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) {
}
}
Your answer
Follow this Question
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