Hi,
I’m implementing an Android application using Unity that will retrieve some data from myserver the first time it’s started. This data has text and images.
In my server I’m using Base64 to code the images and return them in a JSON string. This way I only need a single request to get all the data, but its result is really large. If I try to access WWW.text it takes some seconds to process it.
To parse the JSON I’m using SimpleJSON.JSON.Parse. Since this takes some time to finish I decided to use it in a different Thread.
My problem is accessing WWW.text. If I do it inside the Thread I get this error:
get_isDone can only be called from the main thread
And if I use it outside the Thread my application stops for several seconds.
This is my code:
void OnGUI(){
if(catalogWWW == null){
catalogWWW = new WWW(myURL);
}
if(!catalogWWW.isDone || parseJSONStatus <2)
updateProgressBar();
if(catalogWWW.isDone){
if(parseJSONStatus == 0){
parseJSONStatus = 1;
Thread t = new Thread(parseWWWJSON);
t.Start(catalogWWW);
}
if(parseJSONStatus == 2){
Debug.Log("FINISHED!");
}
}
}
public void parseWWWJSON(object data) {
catalogData = SimpleJSON.JSON.Parse(((WWW)data).text);
parseJSONStatus = 2;
}
With this code I get the error: get_isDone can only be called from the main thread.
If I use t.Start(catalogWWW.text) instead, my progress bar is updated while downloading, then it stops like 5 seconds (catalogWWW.text is being executed) and then it continues to be updated.
How can I fix it?
Thanks!