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”