Hello,
I am trying to download a simple JSON file that is stored in the Streaming Assets Folder in my WEBGL Build. The problem is that I have to wait for the download to finish, since this data is neccessary for starting the game. I've tried many methods of loading the file via UnityWebRequest in a WebGL Development Build, but nothing seems to work - it's just *loading endlessly, without any error message*. The UnityWebRequest.progress that is logged to console is always 0.
Notes: Loading the file in the editor via the File class works fine, so the JSON file is correct. The URL I use is logged to console and if I try to open it in the browser, it shows me the right file, so the URL is correct too.
The approaches I've tried so far are:
----------
**1. Waiting for UnityWebRequest.isDone to be true**
public string Load (string streamingAssetLocalPath)
{
string filePath = Path.Combine (Application.streamingAssetsPath, streamingAssetLocalPath);
UnityWebRequest request = GetRequest (filePath);
return request.downloadHandler.text;
}
private UnityWebRequest GetRequest (string filePath)
{
UnityWebRequest request = UnityWebRequest.Get (filePath);
while (!request.isDone && (request.error == null || request.error.Equals (""))) {
Debug.Log (request.downloadProgress + " - " + request.downloadedBytes);
}
if (request.error != null && !request.error.Equals ("")) {
Debug.LogError ("Request error: " + request.error);
}
return request;
}
----------
**2. Waiting for DownloadHandler.isDone to be true**
Same as above, but with `!request.downloadHandler.isDone` instead of `!request.isDone`.
----------
**3. Waiting for UnityWebRequestAsyncOperation.isDone to be true**
Same as above, but saving the result of `request.SendWebRequest ()` to an UnityWebRequestAsyncOperation and checking for `!asyncOperation.isDone`.
----------
**4. Yielding SendWebRequest**
As far as I know I cannot log the progress with this approach, but this method fails, too: After some time
the console logs an error that the script timed out.
private DownloadHandler downloadHandler;
public string Load (string streamingAssetLocalPath)
{
string filePath = Path.Combine (Application.streamingAssetsPath, streamingAssetLocalPath);
StartCoroutine (SendRequest (filePath));
while (downloadHandler == null) {
Debug.Log ("waiting for " + filePath);
}
string data = downloadHandler.text;
downloadHandler = null;
return data;
}
private IEnumerator SendRequest (string url)
{
using (UnityWebRequest request = UnityWebRequest.Get (url)) {
yield return request.SendWebRequest ();
if (request.isNetworkError || request.isHttpError) {
Debug.LogError ("Request Error: " + request.error);
} else {
downloadHandler = request.downloadHandler;
}
}
}
I have absolutely no idea what I am doing wrong, has anybody had similar problems? Am I overseeing something? Or do I have do use a completely different approach? Any help is appreciated.
↧