I have a problem with Unity WebGL and the way the downloaded data from a UnityWebRequests gets handled. In the standalone version I'm waiting for the downloaded data with while (!async.isDone) { } and then json serialize it. Afterwards I do the yield return null;. This way the data gets processed after the yield return when it got completely got handled first. But in WebGL the while (!async.isDone) { } is not allowed as in the official documents explained WebGL Networking.
So what is the best way to handle this problem? Something like calling the processing in the same coroutine after the handling finished? But then I still have the problem with creating gameObjects with the handled data since you can only create gameObjects in the main thread of unity itself.
Anyone got the same problem and found a solution?
Thanks in advance!
Update: Here is a complete code example:
private IEnumerator m_GetUserToken(string accesscode, string password)
{
UnityWebRequest request = new UnityWebRequest(v_ServerIP + "api-token-auth/", "POST");
byte[] bodyRaw = Encoding.UTF8.GetBytes("{\"username\":\"" + accesscode + "\", \"password\":\"" + password + "\"}");
request.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
request.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", "application/json");
request.chunkedTransfer = false;
UnityWebRequestAsyncOperation async = request.SendWebRequest();
while (!async.isDone) { }
if (request.isNetworkError || request.isHttpError)
{
v_TokenSuccess = false;
Debug.Log("Token Error: " + request.error);
Debug.Log("Token Response Code: " + request.responseCode);
}
else
{
v_UserToken = JsonUtility.FromJson<UserToken>(request.downloadHandler.text);
v_TokenSuccess = true;
}
yield return Timing.WaitForOneFrame;
}
Update2:
Since it is still somehow unclear here is another example:
#Main Thread
string s = "";
StartCoroutine(functionname);
Debug.Log(s);
#Coroutine
private IEnumerator functionname(){
downloadTextfromUrl
while(!downloadFinished) {yield return null}
s = downloadText;
}
So after yield return null is called the Debug.Log(s) gets called and s is still empty since s = downloadText hasn't been called yet.